kpilot

fileInstallWidget.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 **
00005 ** This file defines the internal conduit "File Installer"
00006 ** that accepts drags of URLs containing Palm DBs, prcs, and
00007 ** such. It also does the HotSync part of installing files
00008 ** on the Pilot.
00009 */
00010 
00011 /*
00012 ** This program is free software; you can redistribute it and/or modify
00013 ** it under the terms of the GNU General Public License as published by
00014 ** the Free Software Foundation; either version 2 of the License, or
00015 ** (at your option) any later version.
00016 **
00017 ** This program is distributed in the hope that it will be useful,
00018 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 ** GNU General Public License for more details.
00021 **
00022 ** You should have received a copy of the GNU General Public License
00023 ** along with this program in a file called COPYING; if not, write to
00024 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 ** MA 02110-1301, USA.
00026 */
00027 
00028 /*
00029 ** Bug reports and questions can be sent to kde-pim@kde.org
00030 */
00031 
00032 #ifndef _KPILOT_OPTIONS_H
00033 #include "options.h"
00034 #endif
00035 
00036 #include <unistd.h>
00037 
00038 #include <qlistbox.h>
00039 #include <qstring.h>
00040 #include <qlabel.h>
00041 #include <qpushbutton.h>
00042 #include <qlayout.h>
00043 #include <qwhatsthis.h>
00044 #include <qmultilineedit.h>
00045 #include <qpixmap.h>
00046 #include <qpopupmenu.h>
00047 
00048 #include <kfiledialog.h>
00049 #include <kurldrag.h>
00050 #include <kiconloader.h>
00051 #include <kiconview.h>
00052 #include <kglobal.h>
00053 #include <kurl.h>
00054 
00055 #include "kpilotConfig.h"
00056 #include "fileInstaller.h"
00057 
00058 
00059 #include "fileInstallWidget.moc"
00060 
00061 FileInstallWidget::FileInstallWidget(QWidget * parent,
00062     const QString & path) :
00063     PilotComponent(parent, "component_files", path),
00064     fSaveFileList(false),
00065     fInstaller(0L)
00066 {
00067     FUNCTIONSETUP;
00068 
00069     QGridLayout *grid = new QGridLayout(this, 5, 5, SPACING);
00070 
00071     QLabel *label = new QLabel(i18n("Files to install:"), this);
00072 
00073     grid->addWidget(label, 1, 1);
00074 
00075     QPushButton *abutton;
00076 
00077     abutton = addButton = new QPushButton(i18n("Add File..."), this);
00078     connect(abutton, SIGNAL(clicked()), this, SLOT(slotAddFile()));
00079     grid->addWidget(abutton, 3, 1);
00080     QWhatsThis::add(abutton,
00081         i18n("<qt>Choose a file to add to the list of files to install.</qt>"));
00082 
00083     abutton = clearButton= new QPushButton(i18n("Clear List"), this);
00084     connect(abutton, SIGNAL(clicked()), this, SLOT(slotClearButton()));
00085     grid->addWidget(abutton, 4, 1);
00086     QWhatsThis::add(abutton,
00087         i18n("<qt>Clear the list of files to install. No files will be installed.</qt>"));
00088 
00089     fIconView = new KIconView(this);
00090     connect(fIconView, SIGNAL(dropped(QDropEvent *, const QValueList<QIconDragItem> &)),
00091         this, SLOT(slotDropEvent(QDropEvent *, const QValueList<QIconDragItem> &)));
00092     grid->addMultiCellWidget(fIconView, 1, 4, 2, 3);
00093     QWhatsThis::add(fIconView,
00094         i18n
00095         ("<qt>This lists files that will be installed on the Pilot during the next HotSync. Drag files here or use the Add button.</qt>"));
00096     fIconView->setAcceptDrops(true);
00097     fIconView->setSelectionMode(QIconView::Extended);
00098     fIconView->viewport()->installEventFilter(this);
00099 
00100     grid->setRowStretch(2, 100);
00101     grid->setColStretch(2, 50);
00102     grid->setColStretch(2, 50);
00103     grid->addColSpacing(4, SPACING);
00104     grid->addRowSpacing(5, SPACING);
00105 
00106     fInstaller = new FileInstaller;
00107     connect(fInstaller, SIGNAL(filesChanged()),
00108         this, SLOT(refreshFileInstallList()));
00109 
00110 }
00111 
00112 FileInstallWidget::~FileInstallWidget()
00113 {
00114     KPILOT_DELETE(fInstaller);
00115 }
00116 
00117 static inline bool pdbOrPrc(const QString &s)
00118 {
00119     return s.endsWith(CSL1(".pdb"),false) || s.endsWith(CSL1(".prc"),false) ;
00120 }
00121 
00122 void FileInstallWidget::dragEnterEvent(QDragEnterEvent *event)
00123 {
00124     FUNCTIONSETUP;
00125 
00126     KURL::List urls;
00127     if(!KURLDrag::decode(event, urls)) {
00128         event->accept(false);
00129         return;
00130     }
00131 
00132     KURL::List::const_iterator it;
00133     QString filename;
00134     for ( it = urls.begin(); it != urls.end(); ++it ) {
00135         filename = (*it).fileName();
00136         if(!pdbOrPrc(filename)) {
00137             event->accept(false);
00138             return;
00139         }
00140     }
00141     event->accept(true);
00142 }
00143 
00144 bool FileInstallWidget::eventFilter(QObject *watched, QEvent *event)
00145 {
00146     FUNCTIONSETUP;
00147 
00148     if(watched == fIconView->viewport())
00149     {
00150         if(event->type() == QEvent::DragEnter) {
00151             dragEnterEvent(static_cast<QDragEnterEvent*>(event));
00152             return true;
00153         }
00154 
00155         // We have to skip the DragMove event, because it seems to override the
00156         // accept state, when it is set to false by dragEnterEvent() (event->accept(false);)
00157         if(event->type() == QEvent::DragMove) {
00158             return true;
00159         }
00160 
00161         if(event->type() == QEvent::MouseButtonPress) {
00162             contextMenu(static_cast<QMouseEvent*>(event));
00163         }
00164     }
00165 
00166     return false;
00167 }
00168 
00169 void FileInstallWidget::dropEvent(QDropEvent * drop)
00170 {
00171     FUNCTIONSETUP;
00172     if (!shown) return;
00173 
00174     KURL::List list;
00175 
00176     if (!KURLDrag::decode(drop, list) || list.isEmpty())
00177         return;
00178 
00179 #ifdef DEBUG
00180     DEBUGKPILOT << ": Got " << list.first().prettyURL() << endl;
00181 #endif
00182 
00183     QStringList files;
00184     for(KURL::List::ConstIterator it = list.begin(); it != list.end(); ++it)
00185     {
00186        if ((*it).isLocalFile())
00187           files << (*it).path();
00188     }
00189 
00190     fInstaller->addFiles(files, this );
00191 }
00192 
00193 void FileInstallWidget::slotDropEvent(QDropEvent * drop, const QValueList<QIconDragItem> & /*lst*/)
00194 {
00195     FUNCTIONSETUP;
00196     dropEvent(drop);
00197 }
00198 
00199 void FileInstallWidget::slotClearButton()
00200 {
00201     FUNCTIONSETUP;
00202     fInstaller->clearPending();
00203 }
00204 
00205 void FileInstallWidget::showComponent()
00206 {
00207     FUNCTIONSETUP;
00208     refreshFileInstallList();
00209 }
00210 
00211 void FileInstallWidget::slotAddFile()
00212 {
00213     FUNCTIONSETUP;
00214     if (!shown) return;
00215 
00216     QStringList fileNames = KFileDialog::getOpenFileNames(
00217         QString::null, i18n("*.pdb *.prc|PalmOS Databases (*.pdb *.prc)"));
00218 
00219     for (QStringList::Iterator fileName = fileNames.begin(); fileName != fileNames.end(); ++fileName)
00220     {
00221         fInstaller->addFile(*fileName, this );
00222     }
00223 }
00224 
00225 bool FileInstallWidget::preHotSync(QString &)
00226 {
00227     FUNCTIONSETUP;
00228 
00229     fIconView->setEnabled(false);
00230     fInstaller->setEnabled(false);
00231     addButton->setEnabled(false);
00232     clearButton->setEnabled(false);
00233 
00234     return true;
00235 }
00236 
00237 void FileInstallWidget::postHotSync()
00238 {
00239     FUNCTIONSETUP;
00240 
00241     fInstaller->setEnabled(true);
00242     fIconView->setEnabled(true);
00243     addButton->setEnabled(true);
00244     clearButton->setEnabled(true);
00245     if (shown) refreshFileInstallList();
00246 }
00247 
00248 
00249 void FileInstallWidget::refreshFileInstallList()
00250 {
00251     FUNCTIONSETUP;
00252 
00253     QStringList fileNames = fInstaller->fileNames();
00254     QPixmap kpilotIcon = KGlobal::iconLoader()->loadIcon(CSL1("kpilot"), KIcon::Desktop);
00255 
00256     fIconView->clear();
00257 
00258     for (QStringList::Iterator fileName = fileNames.begin(); fileName != fileNames.end(); ++fileName)
00259     {
00260         if(pdbOrPrc(*fileName))
00261         {
00262             new KIconViewItem(fIconView, *fileName, kpilotIcon);
00263         }
00264         else
00265         {
00266             new KIconViewItem(fIconView, *fileName);
00267         }
00268     }
00269 }
00270 
00271 void FileInstallWidget::contextMenu(QMouseEvent *event)
00272 {
00273     FUNCTIONSETUP;
00274 
00275     if(event->button() == Qt::LeftButton)
00276         return;
00277 
00278     QIconViewItem *item;
00279     QStringList files;
00280     for(item = fIconView->firstItem(); item; item = item->nextItem())
00281     {
00282         if(item->isSelected())
00283             files.append(item->text());
00284     }
00285 
00286     QPopupMenu popup(fIconView);
00287 
00288     item = fIconView->findItem(event->pos());
00289     if(item) {
00290         // Popup for the right clicked item
00291         popup.insertItem(i18n("Delete a single file item","Delete"), 10);
00292     }
00293 
00294     popup.insertItem(i18n("Delete selected files"), 11);
00295     if(files.empty())
00296         popup.setItemEnabled(11, false);
00297 
00298     int id = popup.exec(fIconView->viewport()->mapToGlobal(event->pos()));
00299     if(id == 10)
00300         fInstaller->deleteFile(item->text());
00301     else if(id == 11)
00302         fInstaller->deleteFiles(files);
00303 
00304 }