kpilot/kpilot

fileInstallWidget.cc

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 static const char *fileinstallwidget_id =
00032     "$Id: fileInstallWidget.cc 437980 2005-07-23 19:53:57Z kainhofe $";
00033 
00034 #ifndef _KPILOT_OPTIONS_H
00035 #include "options.h"
00036 #endif
00037 
00038 #include <unistd.h>
00039 
00040 #include <qlistbox.h>
00041 #include <qstring.h>
00042 #include <qlabel.h>
00043 #include <qpushbutton.h>
00044 #include <qlayout.h>
00045 #include <qwhatsthis.h>
00046 #include <qmultilineedit.h>
00047 #include <qpixmap.h>
00048 #include <qpopupmenu.h>
00049 
00050 #include <kfiledialog.h>
00051 #include <kurldrag.h>
00052 #include <kiconloader.h>
00053 #include <kiconview.h>
00054 #include <kglobal.h>
00055 #include <kurl.h>
00056 
00057 #include "kpilotConfig.h"
00058 #include "fileInstaller.h"
00059 
00060 
00061 #include "fileInstallWidget.moc"
00062 
00063 FileInstallWidget::FileInstallWidget(QWidget * parent,
00064     const QString & path) :
00065     PilotComponent(parent, "component_files", path),
00066     fSaveFileList(false),
00067     fInstaller(0L)
00068 {
00069     FUNCTIONSETUP;
00070 
00071     QGridLayout *grid = new QGridLayout(this, 5, 5, SPACING);
00072 
00073     QLabel *label = new QLabel(i18n("Files to install:"), this);
00074 
00075     grid->addWidget(label, 1, 1);
00076 
00077     QPushButton *abutton;
00078 
00079     abutton = addButton = new QPushButton(i18n("Add File..."), this);
00080     connect(abutton, SIGNAL(clicked()), this, SLOT(slotAddFile()));
00081     grid->addWidget(abutton, 3, 1);
00082     QWhatsThis::add(abutton,
00083         i18n("<qt>Choose a file to add to the list of files to install.</qt>"));
00084 
00085     abutton = clearButton= new QPushButton(i18n("Clear List"), this);
00086     connect(abutton, SIGNAL(clicked()), this, SLOT(slotClearButton()));
00087     grid->addWidget(abutton, 4, 1);
00088     QWhatsThis::add(abutton,
00089         i18n("<qt>Clear the list of files to install. No files will be installed.</qt>"));
00090 
00091     fIconView = new KIconView(this);
00092     connect(fIconView, SIGNAL(dropped(QDropEvent *, const QValueList<QIconDragItem> &)),
00093         this, SLOT(slotDropEvent(QDropEvent *, const QValueList<QIconDragItem> &)));
00094     grid->addMultiCellWidget(fIconView, 1, 4, 2, 3);
00095     QWhatsThis::add(fIconView,
00096         i18n
00097         ("<qt>This lists files that will be installed on the Pilot during the next HotSync. Drag files here or use the Add button.</qt>"));
00098     fIconView->setAcceptDrops(true);
00099     fIconView->setSelectionMode(QIconView::Extended);
00100     fIconView->viewport()->installEventFilter(this);
00101 
00102     grid->setRowStretch(2, 100);
00103     grid->setColStretch(2, 50);
00104     grid->setColStretch(2, 50);
00105     grid->addColSpacing(4, SPACING);
00106     grid->addRowSpacing(5, SPACING);
00107 
00108     fInstaller = new FileInstaller;
00109     connect(fInstaller, SIGNAL(filesChanged()),
00110         this, SLOT(refreshFileInstallList()));
00111 
00112     (void) fileinstallwidget_id;
00113 }
00114 
00115 FileInstallWidget::~FileInstallWidget()
00116 {
00117     KPILOT_DELETE(fInstaller);
00118 }
00119 
00120 static inline bool pdbOrPrc(const QString &s)
00121 {
00122     return s.endsWith(CSL1(".pdb"),false) || s.endsWith(CSL1(".prc"),false) ;
00123 }
00124 
00125 void FileInstallWidget::dragEnterEvent(QDragEnterEvent *event)
00126 {
00127     FUNCTIONSETUP;
00128 
00129     KURL::List urls;
00130     if(!KURLDrag::decode(event, urls)) {
00131         event->accept(false);
00132         return;
00133     }
00134 
00135     KURL::List::const_iterator it;
00136     QString filename;
00137     for ( it = urls.begin(); it != urls.end(); ++it ) {
00138         filename = (*it).fileName();
00139         if(!pdbOrPrc(filename)) {
00140             event->accept(false);
00141             return;
00142         }
00143     }
00144     event->accept(true);
00145 }
00146 
00147 bool FileInstallWidget::eventFilter(QObject *watched, QEvent *event)
00148 {
00149     FUNCTIONSETUP;
00150 
00151     if(watched == fIconView->viewport())
00152     {
00153         if(event->type() == QEvent::DragEnter) {
00154             dragEnterEvent(static_cast<QDragEnterEvent*>(event));
00155             return true;
00156         }
00157 
00158         // We have to skip the DragMove event, because it seems to override the
00159         // accept state, when it is set to false by dragEnterEvent() (event->accept(false);)
00160         if(event->type() == QEvent::DragMove) {
00161             return true;
00162         }
00163 
00164         if(event->type() == QEvent::MouseButtonPress) {
00165             contextMenu(static_cast<QMouseEvent*>(event));
00166         }
00167     }
00168 
00169     return false;
00170 }
00171 
00172 void FileInstallWidget::dropEvent(QDropEvent * drop)
00173 {
00174     FUNCTIONSETUP;
00175     if (!shown) return;
00176 
00177     KURL::List list;
00178 
00179     if (!KURLDrag::decode(drop, list) || list.isEmpty())
00180         return;
00181 
00182 #ifdef DEBUG
00183     DEBUGKPILOT << ": Got " << list.first().prettyURL() << endl;
00184 #endif
00185 
00186     QStringList files;
00187     for(KURL::List::ConstIterator it = list.begin(); it != list.end(); ++it)
00188     {
00189        if ((*it).isLocalFile())
00190           files << (*it).path();
00191     }
00192 
00193     fInstaller->addFiles(files, this );
00194 }
00195 
00196 void FileInstallWidget::slotDropEvent(QDropEvent * drop, const QValueList<QIconDragItem> & /*lst*/)
00197 {
00198     FUNCTIONSETUP;
00199     dropEvent(drop);
00200 }
00201 
00202 void FileInstallWidget::slotClearButton()
00203 {
00204     FUNCTIONSETUP;
00205     fInstaller->clearPending();
00206 }
00207 
00208 void FileInstallWidget::showComponent()
00209 {
00210     FUNCTIONSETUP;
00211     refreshFileInstallList();
00212 }
00213 
00214 void FileInstallWidget::slotAddFile()
00215 {
00216     FUNCTIONSETUP;
00217     if (!shown) return;
00218 
00219     QStringList fileNames = KFileDialog::getOpenFileNames(
00220         QString::null, i18n("*.pdb *.prc|PalmOS Databases (*.pdb *.prc)"));
00221 
00222     for (QStringList::Iterator fileName = fileNames.begin(); fileName != fileNames.end(); ++fileName)
00223     {
00224         fInstaller->addFile(*fileName, this );
00225     }
00226 }
00227 
00228 bool FileInstallWidget::preHotSync(QString &)
00229 {
00230     FUNCTIONSETUP;
00231 
00232     fIconView->setEnabled(false);
00233     fInstaller->setEnabled(false);
00234     addButton->setEnabled(false);
00235     clearButton->setEnabled(false);
00236 
00237     return true;
00238 }
00239 
00240 void FileInstallWidget::postHotSync()
00241 {
00242     FUNCTIONSETUP;
00243 
00244     fInstaller->setEnabled(true);
00245     fIconView->setEnabled(true);
00246     addButton->setEnabled(true);
00247     clearButton->setEnabled(true);
00248     if (shown) refreshFileInstallList();
00249 }
00250 
00251 
00252 void FileInstallWidget::refreshFileInstallList()
00253 {
00254     FUNCTIONSETUP;
00255 
00256     QStringList fileNames = fInstaller->fileNames();
00257     QPixmap kpilotIcon = KGlobal::iconLoader()->loadIcon(CSL1("kpilot"), KIcon::Desktop);
00258 
00259     fIconView->clear();
00260 
00261     for (QStringList::Iterator fileName = fileNames.begin(); fileName != fileNames.end(); ++fileName)
00262     {
00263         if(pdbOrPrc(*fileName))
00264         {
00265             new KIconViewItem(fIconView, *fileName, kpilotIcon);
00266         }
00267         else
00268         {
00269             new KIconViewItem(fIconView, *fileName);
00270         }
00271     }
00272 }
00273 
00274 void FileInstallWidget::contextMenu(QMouseEvent *event)
00275 {
00276     FUNCTIONSETUP;
00277 
00278     if(event->button() == Qt::LeftButton)
00279         return;
00280 
00281     QIconViewItem *item;
00282     QStringList files;
00283     for(item = fIconView->firstItem(); item; item = item->nextItem())
00284     {
00285         if(item->isSelected())
00286             files.append(item->text());
00287     }
00288 
00289     QPopupMenu popup(fIconView);
00290 
00291     item = fIconView->findItem(event->pos());
00292     if(item) {
00293         // Popup for the right clicked item
00294         popup.insertItem(TODO_I18N("Delete"), 10);
00295     }
00296 
00297     popup.insertItem(TODO_I18N("Delete selected files"), 11);
00298     if(files.empty())
00299         popup.setItemEnabled(11, false);
00300 
00301     int id = popup.exec(fIconView->viewport()->mapToGlobal(event->pos()));
00302     if(id == 10)
00303         fInstaller->deleteFile(item->text());
00304     else if(id == 11)
00305         fInstaller->deleteFiles(files);
00306 
00307 }
KDE Home | KDE Accessibility Home | Description of Access Keys