uploadwidget.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  * File   : uploadwidget.cpp
00003  * Authors: KIPI team developers (see AUTHORS files for details)
00004  *          
00005  * Date   : 2004-02
00006  * Description :
00007  *
00008  * Copyright 2004 by the KIPI team
00009  *
00010  * This program is free software; you can redistribute it
00011  * and/or modify it under the terms of the GNU Library General
00012  * Public License as published by the Free Software Foundation;
00013  * either version 2, or (at your option)
00014  * any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Library General Public License for more details.
00020  *
00021  * ============================================================ */
00022 
00023 // Qt includes. 
00024   
00025 #include <qlayout.h>
00026 #include <qheader.h>
00027 #include <qlistview.h>
00028 #include <qdir.h>
00029 
00030 // KDE includes
00031 
00032 #include <kdebug.h>
00033 #include <klocale.h>
00034 #include <kio/jobclasses.h>
00035 #include <kmessagebox.h>
00036 
00037 #include <kdeversion.h>
00038 #if KDE_VERSION >= 0x30200
00039 #include <kinputdialog.h>
00040 #else
00041 #include <klineeditdlg.h>
00042 #define KInputDialog KLineEditDlg
00043 #endif
00044 
00045 // Local includes.
00046 
00047 #include "uploadwidget.h"
00048 #include "libkipi/imagecollection.h"
00049 
00050 
00051 struct KIPI::UploadWidget::Private
00052 {
00053     KFileTreeView* m_treeView;
00054     KFileTreeBranch* m_item;
00055     QStringList m_pendingPath;
00056     QString m_handled;
00057 };
00058 
00059 
00065 KIPI::UploadWidget::UploadWidget( KIPI::Interface* interface, QWidget* parent, const char* name )
00066                   : QWidget( parent, name )
00067 {
00068     d = new Private;
00069 
00070     QVBoxLayout* layout = new QVBoxLayout( this, 0 );
00071     d->m_treeView = new KFileTreeView( this );
00072     d->m_treeView->setRootIsDecorated( true );
00073     layout->addWidget( d->m_treeView );
00074 
00075     // Fetch the current album, so we can start out there.
00076     KIPI::ImageCollection album = interface->currentAlbum();
00077     
00078     // If no current album selected, get the first album in the list.
00079     if ( !album.isValid() || !album.isDirectory() ) 
00080        album = interface->allAlbums().first();
00081     
00082     d->m_item = d->m_treeView->addBranch( QDir::cleanDirPath(album.uploadRoot().path()),
00083                                           album.uploadRootName() );
00084     d->m_treeView->setDirOnlyMode( d->m_item, true );
00085 
00086     d->m_treeView->addColumn( i18n("Folder" ) );
00087 
00088     d->m_treeView->header()->setStretchEnabled( true, 0 );
00089 
00090     QString root = album.uploadRoot().path();
00091     QString uploadPath = album.isDirectory() ? album.uploadPath().path() : root;
00092 
00093     root       = QDir::cleanDirPath(root);
00094     uploadPath = QDir::cleanDirPath(uploadPath);
00095     
00096     if ( !uploadPath.startsWith( root ) ) 
00097     {
00098         kdWarning(51000) << "Error in Host application: uploadPath() should start with uploadRoot()." << endl
00099                          << "uploadPath() = " << album.uploadPath().prettyURL() << endl
00100                          << "uploadRoot() = " << album.uploadRoot().prettyURL() << endl;
00101     }
00102     else
00103     {
00104         uploadPath = uploadPath.mid( root.length() );
00105         
00106         d->m_pendingPath = QStringList::split( "/", uploadPath, false );
00107         
00108         connect( d->m_item, SIGNAL( populateFinished(KFileTreeViewItem *) ),
00109                  this, SLOT( load() ) );
00110 
00111         d->m_item->setOpen(true);
00112     }
00113     
00114     connect( d->m_treeView, SIGNAL( executed(QListViewItem *) ),
00115              this, SLOT( slotFolderSelected(QListViewItem *) ) );
00116 }
00117 
00118 KIPI::UploadWidget::~UploadWidget()
00119 {
00120     delete d;
00121 }
00122 
00123 KURL KIPI::UploadWidget::path() const
00124 {
00125     return d->m_treeView->currentURL();
00126 }
00127 
00128 void KIPI::UploadWidget::load()
00129 {
00130     if ( d->m_pendingPath.isEmpty() ) 
00131     {
00132         disconnect( d->m_item, SIGNAL( populateFinished(KFileTreeViewItem *) ), 
00133                     this, SLOT( load() ) );
00134         return;
00135     }
00136 
00137     QString item = d->m_pendingPath.front();
00138 
00139     d->m_pendingPath.pop_front();
00140 
00141     d->m_handled += "/" + item;
00142 
00143     KFileTreeViewItem* branch = d->m_treeView->findItem( d->m_item, d->m_handled );
00144     
00145     if ( !branch ) 
00146     {
00147         kdDebug( 51000 ) << "Unable to open " << d->m_handled << endl;
00148     }
00149     else
00150     {
00151         branch->setOpen( true );
00152         d->m_treeView->setSelected( branch, true );
00153         d->m_treeView->ensureItemVisible ( branch );
00154         
00155         if ( branch->alreadyListed() )
00156             load();
00157     }
00158 
00159 }
00160 
00161 void KIPI::UploadWidget::mkdir()
00162 {
00163     if ( !path().isValid() ) 
00164         {
00165         KMessageBox::error( this, i18n("Please select a directory first.") );
00166         return;
00167         }
00168 
00169     bool ok;
00170     QString dir = KInputDialog::getText( i18n("Create Directory"),
00171                                          i18n("<qt>Enter new directory name (to be created as subdir of %1):</qt>")
00172                                         .arg(path().prettyURL()), "", &ok, this);
00173     
00174     if (!ok) return;
00175 
00176     KURL url = path();
00177     url.addPath( dir );
00178 
00179     KIO::SimpleJob* job = KIO::mkdir(url);
00180     
00181     connect(job, SIGNAL(result(KIO::Job*)), 
00182             this, SLOT(slotAlbumCreated(KIO::Job*)));
00183 }
00184 
00185 void KIPI::UploadWidget::slotAlbumCreated(KIO::Job* job)
00186 {
00187     int code = job->error();
00188     
00189     if ( code )
00190         job->showErrorDialog( this );
00191 }
00192 
00193 void KIPI::UploadWidget::slotFolderSelected(QListViewItem *)
00194 {
00195     emit folderItemSelected(d->m_treeView->currentURL());
00196 }
00197 
00198 #include "uploadwidget.moc"

Generated on Mon Nov 27 23:42:22 2006 for libKipi by  doxygen 1.5.0