KDevelop API Documentation

addexistingfilesdlg.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                              -------------------
00003     begin                : Frag' mich was leichteres
00004     copyright            : (C) 2002 by Victor Rder
00005     email                : victor_roeder@gmx.de
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00019 #include <qapplication.h>
00020 #include <qgroupbox.h>
00021 #include <qlabel.h>
00022 //#include <qlistbox.h>
00023 #include <qpushbutton.h>
00024 #include <qlayout.h>
00025 #include <qtooltip.h>
00026 #include <qwhatsthis.h>
00027 #include <qpixmap.h>
00028 
00029 #include <kdebug.h>
00030 #include <kfileview.h>
00031 #include <kguiitem.h>
00032 #include <kprogress.h>
00033 #include <kprocess.h>
00034 #include <kurldrag.h>
00035 #include <kmessagebox.h>
00036 #include <kmimetype.h>
00037 #include <kiconview.h>
00038 #include <ksqueezedtextlabel.h>
00039 
00040 #include "autolistviewitems.h"
00041 
00042 #include "autoprojectwidget.h"
00043 #include "autoprojectpart.h"
00044 
00045 #include "kimporticonview.h"
00046 
00047 #include "addexistingfilesdlg.h"
00048 
00049 /*
00050  *  Constructs a AddExistingFilesDialog which is a child of 'parent', with the
00051  *  name 'name' and widget flags set to 'f'.
00052  *
00053  *  The dialog will by default be modeless, unless you set 'modal' to
00054  *  TRUE to construct a modal dialog.
00055  */
00056 
00057 AddExistingFilesDialog::AddExistingFilesDialog ( AutoProjectPart* part, AutoProjectWidget *widget, SubprojectItem* spitem, TargetItem* titem, QWidget* parent, const char* name, bool modal, WFlags fl )
00058         : AddExistingDlgBase ( parent, name, modal, fl )
00059 {
00060     m_spitem = spitem;
00061     m_titem = titem;
00062 
00063     m_part = part;
00064     m_widget = widget;
00065 
00066     KFile::Mode mode = KFile::Files;
00067 
00068     if ( titem && spitem && titem->type() == ProjectItem::Target && spitem->type() == ProjectItem::Subproject )
00069     {
00070 
00071         if ( titem->name.isEmpty() )
00072         {
00073             QString target = i18n ( "%1 in %2" ).arg ( titem->primary ).arg ( titem->prefix );
00074             targetLabel->setText ( target );
00075         }
00076         else
00077         {
00078             targetLabel->setText ( titem->name );
00079         }
00080         directoryLabel->setText ( spitem->path );
00081     }
00082 
00083     sourceSelector = new FileSelectorWidget ( m_part, mode, sourceGroupBox, "source file selector" );
00084     sourceGroupBoxLayout->addWidget ( sourceSelector );
00085 
00086     importView = new KImportIconView ( i18n ( "Drag one or more files from the left view and drop it here." ), destGroupBox, "destination icon view" );
00087     destGroupBoxLayout->addWidget ( importView );
00088     //destGroupBoxLayout->setStretchFactor(dir, 2);
00089 
00090     QWidget::setTabOrder(sourceSelector, addAllButton);
00091     QWidget::setTabOrder(addAllButton, addSelectedButton);
00092     QWidget::setTabOrder(addSelectedButton, importView);
00093     QWidget::setTabOrder(importView, removeAllButton);
00094     QWidget::setTabOrder(removeAllButton, removeSelectedButton);
00095     QWidget::setTabOrder(removeSelectedButton, okButton);
00096     QWidget::setTabOrder(okButton, cancelButton);
00097 
00098     sourceSelector->setFocus();
00099 
00100     setIcon ( SmallIcon ( "fileimport.png" ) );
00101 
00102     init();
00103 }
00104 
00105 
00106 /*
00107  *  Destroys the object and frees any allocated resources
00108  */
00109 AddExistingFilesDialog::~AddExistingFilesDialog()
00110 {
00111     // no need to delete child widgets, Qt does it all for me
00112 }
00113 
00114 
00115 // void AddExistingFilesDialog::resizeEvent ( QResizeEvent* ev )
00116 // {
00117 //     AddExistingDlgBase::resizeEvent ( ev );
00118 //     //importView->update();
00119 // }
00120 
00121 
00122 void AddExistingFilesDialog::init()
00123 {
00124     progressBar->hide();
00125 
00126     importView->setMode ( KIconView::Select );
00127     importView->setItemsMovable ( false );
00128 
00129     connect ( okButton, SIGNAL ( clicked () ), this, SLOT ( slotOk () ) );
00130 
00131     connect ( addSelectedButton, SIGNAL ( clicked () ), this, SLOT ( slotAddSelected() ) );
00132     connect ( addAllButton, SIGNAL ( clicked () ), this, SLOT ( slotAddAll() ) );
00133     connect ( removeSelectedButton, SIGNAL ( clicked () ), this, SLOT ( slotRemoveSelected() ) );
00134     connect ( removeAllButton, SIGNAL ( clicked () ), this, SLOT ( slotRemoveAll() ) );
00135 
00136     connect ( importView, SIGNAL ( dropped( QDropEvent* ) ), this, SLOT ( slotDropped ( QDropEvent* ) ) );
00137 
00138     importView->setSelectionMode ( KFile::Multi );
00139 
00140     Q_ASSERT( m_spitem );
00141     sourceSelector->setDir ( m_spitem->path );
00142 }
00143 
00144 void AddExistingFilesDialog::importItems()
00145 {
00146     if( !importView->items() )
00147     return;
00148 
00149     // items added via button or drag 'n drop
00150     KFileItemListIterator itemList ( m_importList );
00151 
00152     // items already added to the importView
00153     KFileItemListIterator importedList ( *importView->items() );
00154 
00155     QListViewItem* child = m_titem->firstChild();
00156 
00157     QStringList duplicateList;
00158 
00159     while ( child )
00160     {
00161         FileItem* curItem = static_cast<FileItem*> ( child );
00162 
00163         itemList.toFirst();
00164 
00165         for ( ; itemList.current(); ++itemList )
00166         {
00167             if ( ( *itemList )->name() == curItem->name )
00168             {
00169                 duplicateList.append ( ( *itemList )->name() );
00170                 m_importList.remove ( ( *itemList ) );
00171             }
00172         }
00173 
00174         child = child->nextSibling();
00175     }
00176 
00177     importedList.toFirst();
00178 
00179     for ( ; importedList.current(); ++importedList )
00180     {
00181         itemList.toFirst();
00182 
00183         for ( ; itemList.current(); ++itemList )
00184         {
00185             if ( ( *importedList )->name() == ( *itemList )->name() )
00186             {
00187                 m_importList.remove ( ( *itemList ) );
00188 
00189                 // to avoid that a item is added twice
00190                 if ( !duplicateList.remove ( ( *importedList )->name() ) )
00191                 {
00192                     duplicateList.append ( ( *importedList )->name() );
00193                 }
00194             }
00195         }
00196     }
00197 
00198     if ( duplicateList.count() > 0 )
00199     {
00200         if ( KMessageBox::warningContinueCancelList ( this, i18n (
00201                                                                         "The following file(s) already exist(s) in the target!\n"
00202                                                                         "Press Continue to import only the new files.\n"
00203                                                                         "Press Cancel to abort the complete import." ),
00204                                                                         duplicateList, "Warning", KGuiItem ( i18n ( "Continue" ) ) ) == KMessageBox::Cancel )
00205         {
00206             m_importList.clear();
00207             return;
00208         }
00209     }
00210 
00211     itemList.toFirst();
00212 
00213     for ( ; itemList.current(); ++itemList )
00214     {
00215         if ( !( *itemList )->isDir() )
00216         {
00217             importView->insertItem ( ( *itemList ) );
00218         }
00219     }
00220 
00221     importView->somethingDropped ( true );
00222 
00223     m_importList.clear();
00224 
00225     importView->update ();
00226 }
00227 
00228 void AddExistingFilesDialog::slotOk()
00229 {
00230     if ( importView->items()->count() == 0 ) QDialog::reject();
00231 
00232     progressBar->show();
00233     progressBar->setFormat ( i18n ( "Importing... %p%" ) );
00234 
00235     qApp->processEvents();
00236 
00237     KFileItemListIterator items ( *importView->items() );
00238 
00239     // contains at the end only the imported files outside the subproject directory
00240     KFileItemList outsideList;
00241 
00242     QStringList stringList;
00243 
00244     for ( ; items.current(); ++items )
00245     {
00246         // kdDebug ( 9000 ) << " **** " << (  *items )->url().directory() << "***** " << m_spitem->path << endl;
00247         if ( (  *items )->url().directory() != m_spitem->path )
00248         {
00249             stringList.append ( ( *items )->name() );
00250             outsideList.append ( ( *items ) );
00251         }
00252     }
00253 
00254     progressBar->setTotalSteps ( outsideList.count() + importView->items()->count() );
00255 
00256     if ( outsideList.count() > 0 )
00257     {
00258         if ( KMessageBox::questionYesNoList ( this, i18n (
00259                                 "The following file(s) are not in the Subproject directory.\n"
00260                                 "Press Link to add the files by creating symbolic links.\n"
00261                                 "Press Copy to copy the files into the directory." ),
00262                                 stringList, i18n("Warning"), KGuiItem ( i18n ( "Link (recommended)" ) ), KGuiItem ( i18n ( "Copy (not recommended)" ) ) ) == KMessageBox::No )
00263         {
00264             // Copy files into the Subproject directory
00265             KFileItemListIterator it ( outsideList ) ;
00266 
00267             for ( ; it.current(); ++it )
00268             {
00269                 KProcess proc;
00270 
00271                 proc << "cp";
00272                 proc << ( *it )->url().path();
00273                 proc << m_spitem->path;
00274                 proc.start(KProcess::DontCare);
00275 
00276                 progressBar->setValue ( progressBar->value() + 1 );
00277             }
00278         }
00279         else
00280         {
00281             // Link them into the Subproject directory
00282             KFileItemListIterator it ( outsideList ) ;
00283 
00284             for ( ; it.current(); ++it )
00285             {
00286                 KProcess proc;
00287 
00288                 proc << "ln";
00289                 proc << "-s";
00290                 proc << ( *it )->url().path();
00291                 proc << m_spitem->path;
00292                 proc.start(KProcess::DontCare);
00293 
00294                 progressBar->setValue ( progressBar->value() + 1 );
00295             }
00296         }
00297     }
00298 
00299     items.toFirst();
00300 
00301     QString canontargetname = AutoProjectTool::canonicalize ( m_titem->name );
00302         QString varname;
00303         if( m_titem->primary == "PROGRAMS" || m_titem->primary == "LIBRARIES" || m_titem->primary == "LTLIBRARIES" )
00304             varname = canontargetname + "_SOURCES";
00305         else
00306             varname = m_titem->prefix + "_" + m_titem->primary;
00307 
00308     QMap<QString,QString> replaceMap;
00309     FileItem* fitem = 0L;
00310     QStringList fileList;
00311 
00312     for ( ; items.current(); ++items )
00313     {
00314         m_spitem->variables [ varname ] += ( " " + ( *items )->name() );
00315         replaceMap.insert ( varname, m_spitem->variables [ varname ] );
00316 
00317         fitem = m_widget->createFileItem ( ( *items )->name(), m_spitem );
00318         m_titem->sources.append ( fitem );
00319         m_titem->insertItem ( fitem );
00320 
00321         fileList.append ( m_spitem->path.mid ( m_part->projectDirectory().length() + 1 ) + "/" + ( *items )->name() );
00322 
00323         progressBar->setValue ( progressBar->value() + 1 );
00324     }
00325 
00326     m_widget->emitAddedFiles ( fileList );
00327 
00328     AutoProjectTool::modifyMakefileam ( m_spitem->path + "/Makefile.am", replaceMap );
00329 
00330     QDialog::accept();
00331 
00332 }
00333 
00334 void AddExistingFilesDialog::slotAddSelected()
00335 {
00336     KFileItemListIterator it ( *sourceSelector->dirOperator()->selectedItems() );
00337 
00338     for ( ; it.current(); ++it )
00339     {
00340         if ( ( *it )->url().isLocalFile() ) // maybe unnecessary
00341         {
00342             m_importList.append ( ( *it ) );
00343         }
00344     }
00345 
00346     importItems();
00347 }
00348 
00349 
00350 void AddExistingFilesDialog::slotAddAll()
00351 {
00352     KFileItemListIterator it ( *sourceSelector->dirOperator()->view()->items() );
00353 
00354     for ( ; it.current(); ++it )
00355     {
00356         if ( ( *it )->url().isLocalFile() ) // maybe unnecessary
00357         {
00358             m_importList.append ( ( *it ) );
00359         }
00360     }
00361 
00362     importItems();
00363 }
00364 
00365 void AddExistingFilesDialog::slotRemoveAll()
00366 {
00367     KURL::List deletedFiles;
00368     KFileItemListIterator it ( *importView->items() );
00369 
00370     for ( ; it.current(); ++it )
00371     {
00372         kdDebug ( 9000 ) << "AddExistingFilesDialog::slotRemoveAll()" << endl;
00373         //deletedFiles.append ( ( *it )->url() );
00374         if ( (*it ) ) importView->removeItem ( *it );
00375     }
00376 
00377     importView->somethingDropped ( false );
00378 
00379     importView->viewport()->update();
00380 }
00381 
00382 void AddExistingFilesDialog::slotRemoveSelected()
00383 {
00384     KFileItemListIterator items ( *importView->items() );
00385 
00386     KFileItemList* selectedList = (KFileItemList*) importView->selectedItems();
00387 
00388     KFileItem * deleteItem = 0L;
00389 
00390     for ( ; items.current(); ++items )
00391     {
00392         deleteItem = selectedList->first();
00393 
00394         while ( deleteItem )
00395         {
00396             if ( deleteItem == ( *items ) )
00397             {
00398                 importView->removeItem ( deleteItem );
00399                 deleteItem = selectedList->current();
00400             }
00401             else
00402             {
00403                 deleteItem = selectedList->next();
00404             }
00405         }
00406     }
00407 
00408     if ( importView->items()->count() == 0 ) importView->somethingDropped ( false );
00409 
00410     importView->viewport()->update();
00411 }
00412 
00413 
00414 void AddExistingFilesDialog::slotDropped ( QDropEvent* ev )
00415 {
00416     kdDebug ( 9000 ) << "AddExistingFilesDialog::dropped()" << endl;
00417 
00418     KURL::List urls;
00419 
00420     KURLDrag::decode( ev, urls );
00421 
00422     KFileItem* item = 0L;
00423     KMimeType::Ptr type = 0L;
00424 
00425 
00426     for ( KURL::List::Iterator it = urls.begin(); it != urls.end(); ++it )
00427     {
00428         if ( ( *it ).isLocalFile() ) // maybe unnecessary
00429         {
00430             type = KMimeType::findByURL ( ( *it ) );
00431 
00432             if ( type->name() != KMimeType::defaultMimeType() )
00433             {
00434                 item = new KFileItem ( ( *it ) , type->name(), 0 );
00435             }
00436             else
00437             {
00438                 // take a text-file-icon instead of the ugly question-mark-icon
00439                 item = new KFileItem ( ( *it ), "text/plain", 0 );
00440             }
00441 
00442             m_importList.append ( item );
00443         }
00444     }
00445 
00446     importItems();
00447 }
00448 
00449 #include "addexistingfilesdlg.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:21 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003