* don't show not yet implemented features in the config dialog
[kdenetwork.git] / kget / mainwindow.cpp
blob8e8b5d6cfa1443546b98f69bc2c208e394c03bf0
1 /* This file is part of the KDE project
3 Copyright (C) 2002 by Patrick Charbonnier <pch@freeshell.org>
4 Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
5 Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
6 Copyright (C) 2006, 2007 Urs Wolfer <uwolfer @ kde.org>
7 Copyright (C) 2006 Dario Massarin <nekkar@libero.it>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
15 #include "mainwindow.h"
17 #include "core/kget.h"
18 #include "core/transferhandler.h"
19 #include "core/transfergrouphandler.h"
20 #include "dbus/dbusmodelobserver.h"
21 #include "settings.h"
22 #include "conf/preferencesdialog.h"
23 #include "ui/viewscontainer.h"
24 #include "ui/tray.h"
25 #include "ui/droptarget.h"
26 #include "ui/newtransferdialog.h"
28 #include <kapplication.h>
29 #include <kstandarddirs.h>
30 #include <KInputDialog>
31 #include <kmessagebox.h>
32 #include <kshortcutsdialog.h>
33 #include <kedittoolbar.h>
34 #include <knotifyconfigwidget.h>
35 #include <kfiledialog.h>
36 #include <ktoolinvocation.h>
37 #include <kmenubar.h>
38 #include <kiconloader.h>
39 #include <kstandardaction.h>
40 #include <klocale.h>
41 #include <kicon.h>
42 #include <kactionmenu.h>
44 #include <QtDBus>
45 #include <QClipboard>
46 #include <QTimer>
48 MainWindow::MainWindow(bool showMainwindow, bool startWithoutAnimation, QWidget *parent)
49 : KXmlGuiWindow( parent ),
50 m_drop(0), m_dock(0), m_startWithoutAnimation(startWithoutAnimation)
52 resize(720, 380); // have a reasonable initial size, will be overwritten later if the user changes it
54 // create the model
55 KGet::self( this );
57 // create actions
58 setupActions();
60 createGUI("kgetui.rc");
62 m_viewsContainer = new ViewsContainer(this);
63 // initialize the model observer to export percents over dbus
64 m_dbusModelObserver = new DBusModelObserver();
66 setCentralWidget(m_viewsContainer);
68 // restore position, size and visibility
69 move( Settings::mainPosition() );
70 setAutoSaveSettings();
71 setPlainCaption(i18n("KGet"));
73 if ( Settings::showMain() && showMainwindow)
74 show();
75 else
76 hide();
78 //Some of the widgets are initialized in slotDelayedInit()
79 QTimer::singleShot( 0, this, SLOT(slotDelayedInit()) );
82 MainWindow::~MainWindow()
84 //Save the user's transfers
85 KGet::save();
87 slotSaveMyself();
88 delete clipboardTimer;
89 delete m_drop;
90 delete m_dock;
91 // reset konqueror integration (necessary if user enabled / disabled temporarily integration from tray)
92 slotKonquerorIntegration( Settings::konquerorIntegration() );
93 // the following call saves options set in above dtors
94 Settings::self()->writeConfig();
98 void MainWindow::setupActions()
100 QAction *newDownloadAction = actionCollection()->addAction("new_download");
101 newDownloadAction->setText(i18n("&New Download..."));
102 newDownloadAction->setIcon(KIcon("document-new"));
103 newDownloadAction->setShortcuts(KShortcut("Ctrl+N"));
104 connect(newDownloadAction, SIGNAL(triggered()), SLOT(slotNewTransfer()));
106 QAction *openAction = actionCollection()->addAction("import_transfers");
107 openAction->setText(i18n("&Import Transfers..."));
108 openAction->setIcon(KIcon("document-open"));
109 openAction->setShortcuts(KShortcut("Ctrl+I"));
110 connect(openAction, SIGNAL(triggered()), SLOT(slotImportTransfers()));
112 QAction *exportAction = actionCollection()->addAction("export_transfers");
113 exportAction->setText(i18n("&Export Transfers List..."));
114 exportAction->setIcon(KIcon("document-export"));
115 exportAction->setShortcuts(KShortcut("Ctrl+E"));
116 connect(exportAction, SIGNAL(triggered()), SLOT(slotExportTransfers()));
118 QAction *deleteGroupAction = actionCollection()->addAction("delete_groups");
119 deleteGroupAction->setText(i18n("Delete Group"));
120 deleteGroupAction->setIcon(KIcon("edit-delete"));
121 connect(deleteGroupAction, SIGNAL(triggered()), SLOT(slotDeleteGroup()));
123 QAction *renameGroupAction = actionCollection()->addAction("rename_groups");
124 renameGroupAction->setText(i18n("Rename Group"));
125 renameGroupAction->setIcon(KIcon("editinput"));
126 connect(renameGroupAction, SIGNAL(triggered()), SLOT(slotRenameGroup()));
128 m_autoPasteAction = new KToggleAction(KIcon("klipper"),
129 i18n("Auto-Paste Mode"), actionCollection());
130 actionCollection()->addAction("auto_paste", m_autoPasteAction);
131 m_autoPasteAction->setChecked(Settings::autoPaste());
132 m_autoPasteAction->setWhatsThis(i18n("<b>Auto paste</b> button toggles the auto-paste mode "
133 "on and off.\nWhen set, KGet will periodically scan "
134 "the clipboard for URLs and paste them automatically."));
135 connect(m_autoPasteAction, SIGNAL(triggered()), SLOT(slotToggleAutoPaste()));
137 m_konquerorIntegration = new KToggleAction(KIcon("konqueror"),
138 i18n("Use KGet as Konqueror Download Manager"), actionCollection());
139 actionCollection()->addAction("konqueror_integration", m_konquerorIntegration);
140 connect(m_konquerorIntegration, SIGNAL(triggered(bool)), SLOT(slotTrayKonquerorIntegration(bool)));
141 m_konquerorIntegration->setChecked(Settings::konquerorIntegration());
143 // local - Destroys all sub-windows and exits
144 QAction *quitAction = KStandardAction::quit(this, SLOT(slotQuit()), actionCollection());
145 actionCollection()->addAction("quit", quitAction);
146 // local - Standard configure actions
147 QAction *preferencesAction = KStandardAction::preferences(this, SLOT(slotPreferences()), actionCollection());
148 actionCollection()->addAction("preferences", preferencesAction);
149 QAction *configToolbarAction = KStandardAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection());
150 actionCollection()->addAction("configure_toolbars", configToolbarAction);
151 QAction *keyBindingsAction = KStandardAction::keyBindings(this, SLOT(slotConfigureKeys()), actionCollection());
152 actionCollection()->addAction("configure_keys", keyBindingsAction);
153 QAction *cinfigNotifyAction = KStandardAction::configureNotifications(this, SLOT(slotConfigureNotifications()), actionCollection());
154 actionCollection()->addAction("configure_notifications", cinfigNotifyAction);
155 m_menubarAction = KStandardAction::showMenubar(this, SLOT(slotShowMenubar()), actionCollection());
156 m_menubarAction->setChecked(!menuBar()->isHidden());
157 actionCollection()->addAction("settings_showmenubar", m_menubarAction);
159 // Transfer related actions
160 QAction *deleteSelectedAction = actionCollection()->addAction("delete_selected_download");
161 deleteSelectedAction->setText(i18n("Delete Selected"));
162 deleteSelectedAction->setIcon(KIcon("edit-delete"));
163 deleteSelectedAction->setShortcuts(KShortcut("Del"));
164 connect(deleteSelectedAction, SIGNAL(triggered()), SLOT(slotDeleteSelected()));
166 QAction *startAllAction = actionCollection()->addAction("start_all_download");
167 startAllAction->setText(i18n("Start / Resume All"));
168 startAllAction->setIcon(KIcon("media-seek-forward"));
169 startAllAction->setShortcuts(KShortcut("Ctrl+R"));
170 connect(startAllAction, SIGNAL(triggered()), SLOT(slotStartAllDownload()));
172 QAction *startSelectedAction = actionCollection()->addAction("start_selected_download");
173 startSelectedAction->setText(i18n("Start / Resume Selected"));
174 startSelectedAction->setIcon(KIcon("media-playback-start"));
175 connect(startSelectedAction, SIGNAL(triggered()), SLOT(slotStartSelectedDownload()));
177 QAction *stopAllAction = actionCollection()->addAction("stop_all_download");
178 stopAllAction->setText(i18n("Stop All"));
179 stopAllAction->setIcon(KIcon("media-playback-pause"));
180 stopAllAction->setShortcuts(KShortcut("Ctrl+P"));
181 connect(stopAllAction, SIGNAL(triggered()), SLOT(slotStopAllDownload()));
183 QAction *stopSelectedAction = actionCollection()->addAction("stop_selected_download");
184 stopSelectedAction->setText(i18n("Stop Selected"));
185 stopSelectedAction->setIcon(KIcon("media-playback-pause"));
186 connect(stopSelectedAction, SIGNAL(triggered()), SLOT(slotStopSelectedDownload()));
188 KActionMenu *startActionMenu = new KActionMenu(KIcon("media-playback-start"), i18n("Start / Resume"),
189 actionCollection());
190 actionCollection()->addAction("start_menu", startActionMenu);
191 startActionMenu->setDelayed(true);
192 startActionMenu->addAction(startAllAction);
193 startActionMenu->addAction(startSelectedAction);
194 connect(startActionMenu, SIGNAL(triggered()), SLOT(slotStartDownload()));
196 KActionMenu *stopActionMenu = new KActionMenu(KIcon("media-playback-pause"), i18n("Stop"),
197 actionCollection());
198 actionCollection()->addAction("stop_menu", stopActionMenu);
199 stopActionMenu->setDelayed(true);
200 stopActionMenu->addAction(stopAllAction);
201 stopActionMenu->addAction(stopSelectedAction);
202 connect(stopActionMenu, SIGNAL(triggered()), SLOT(slotStopDownload()));
204 QAction *openDestAction = actionCollection()->addAction("transfer_open_dest");
205 openDestAction->setText(i18n("Open Destination"));
206 openDestAction->setIcon(KIcon("folder"));
207 connect(openDestAction, SIGNAL(triggered()), SLOT(slotTransfersOpenDest()));
209 QAction *showDetailsAction = actionCollection()->addAction("transfer_show_details");
210 showDetailsAction->setText(i18n("Show Details"));
211 showDetailsAction->setIcon(KIcon("configure"));
212 connect(showDetailsAction, SIGNAL(triggered()), SLOT(slotTransfersShowDetails()));
214 QAction *copyUrlAction = actionCollection()->addAction("transfer_copy_source_url");
215 copyUrlAction->setText(i18n("Copy URL to Clipboard"));
216 copyUrlAction->setIcon(KIcon("klipper"));
217 connect(copyUrlAction, SIGNAL(triggered()), SLOT(slotTransfersCopySourceUrl()));
219 KToggleAction *showDropTargetAction = new KToggleAction(KIcon("kget"),
220 i18n("Show Drop Target"), actionCollection());
221 actionCollection()->addAction("show_drop_target", showDropTargetAction);
222 showDropTargetAction->setChecked(Settings::showDropTarget());
223 connect(showDropTargetAction, SIGNAL(triggered()), SLOT(slotToggleDropTarget()));
226 void MainWindow::slotDelayedInit()
228 //Here we import the user's transfers.
229 KGet::load( KStandardDirs::locateLocal("appdata", "transfers.kgt") );
231 m_dock = new Tray(this);
232 if(Settings::enableSystemTray()) {
233 // DockWidget
234 m_dock->show();
237 // enable dropping
238 setAcceptDrops(true);
240 // enable hide toolbar
241 setStandardToolBarMenuEnabled(true);
243 // session management stuff
244 connect(kapp, SIGNAL(saveYourself()), SLOT(slotSaveMyself()));
246 // set auto-resume in kioslaverc (is there a cleaner way?)
247 KConfig cfg("kioslaverc", KConfig::NoGlobals);
248 cfg.group(QString()).writeEntry("AutoResume", true);
249 cfg.sync();
251 // immediately start downloading if configured this way
252 if ( Settings::downloadAtStartup() )
253 slotStartDownload();
255 // DropTarget
256 m_drop = new DropTarget(this);
258 if (Settings::firstRun()) {
259 if (KMessageBox::questionYesNoCancel(this ,i18n("This is the first time you have run KGet.\n"
260 "Would you like to enable KGet as the download manager for Konqueror?"),
261 i18n("Konqueror Integration"), KGuiItem(i18n("Enable"), KIcon("dialog-apply")),
262 KGuiItem(i18n("Do Not Enable"), KIcon("edit-delete")))
263 == KMessageBox::Yes) {
264 Settings::setKonquerorIntegration(true);
265 slotKonquerorIntegration(true);
268 m_drop->setVisible(true);
270 // reset the FirstRun config option
271 Settings::setFirstRun(false);
274 if (Settings::showDropTarget() && !m_startWithoutAnimation)
275 m_drop->setVisible(true);
277 //auto paste stuff
278 lastClipboard = QApplication::clipboard()->text( QClipboard::Clipboard ).trimmed();
279 clipboardTimer = new QTimer(this);
280 connect(clipboardTimer, SIGNAL(timeout()), SLOT(slotCheckClipboard()));
281 if ( Settings::autoPaste() )
282 clipboardTimer->start(1000);
285 void MainWindow::slotToggleDropTarget()
287 actionCollection()->action("show_drop_target")->setChecked(!m_drop->isVisible());
289 m_drop->setVisible(!m_drop->isVisible());
292 void MainWindow::slotNewTransfer()
294 NewTransferDialog::showNewTransferDialog();
297 void MainWindow::slotImportTransfers()
299 QString filename = KFileDialog::getOpenFileName(KUrl(),
300 "*.kgt *.metalink|" + i18n("All Openable Files") +
301 " (*.kgt *.metalink)", this, i18n("Open File"));
303 if(filename.endsWith(".kgt"))
305 KGet::load(filename);
306 return;
309 if(!filename.isEmpty())
310 KGet::addTransfer( KUrl( filename ) );
313 void MainWindow::slotQuit()
315 if (KGet::schedulerRunning()) {
316 if (KMessageBox::warningYesNoCancel(this,
317 i18n("Some transfers are still running.\n"
318 "Are you sure you want to close KGet?"),
319 i18n("Confirm Quit"),
320 KStandardGuiItem::yes(), KStandardGuiItem::no(), KStandardGuiItem::cancel(),
321 "ExitWithActiveTransfers") != KMessageBox::Yes)
322 return;
324 KGet::setSchedulerRunning(false);
327 Settings::self()->writeConfig();
328 qApp->quit();
331 void MainWindow::slotPreferences()
333 // an instance the dialog could be already created and could be cached,
334 // in which case you want to display the cached dialog
335 if ( PreferencesDialog::showDialog( "preferences" ) )
336 return;
338 // we didn't find an instance of this dialog, so lets create it
339 PreferencesDialog * dialog = new PreferencesDialog( this, Settings::self() );
340 KGet::setPluginsSettingsWidget( dialog->pluginsWidget() );
341 // keep us informed when the user changes settings
342 connect( dialog, SIGNAL(settingsChanged(const QString&)),
343 this, SLOT(slotNewConfig()) );
345 dialog->show();
348 void MainWindow::slotExportTransfers()
350 QString filename = KFileDialog::getSaveFileName
351 (KUrl(),
352 "*.kgt|" + i18n("KGet Transfer List") + " (*.kgt)",
353 this,
354 i18n("Export Transfers")
357 if(!filename.isEmpty())
358 KGet::save(filename);
361 void MainWindow::slotDeleteGroup()
363 foreach(TransferGroupHandler * it, KGet::selectedTransferGroups())
365 it->stop();
366 KGet::delGroup(it->name());
370 void MainWindow::slotRenameGroup()
372 bool ok = true;
373 QString groupName;
375 foreach(TransferGroupHandler * it, KGet::selectedTransferGroups())
377 groupName = KInputDialog::getText(i18n("Enter Group Name"),
378 i18n("Group name:"), it->name(), &ok, this);
379 if(ok)
380 it->setName(groupName);
384 void MainWindow::slotStartDownload()
386 if(KGet::selectedTransfers().size() == 0)
387 slotStartAllDownload();
388 else
389 slotStartSelectedDownload();
392 void MainWindow::slotStartAllDownload()
394 m_dock->setDownloading(true);
396 KGet::setSchedulerRunning(true);
399 void MainWindow::slotStartSelectedDownload()
401 m_dock->setDownloading(true);
403 foreach(TransferHandler * it, KGet::selectedTransfers())
404 it->start();
407 void MainWindow::slotStopDownload()
409 if(KGet::selectedTransfers().size() == 0)
410 slotStopAllDownload();
411 else
412 slotStopSelectedDownload();
415 void MainWindow::slotStopAllDownload()
417 m_dock->setDownloading(false);
419 KGet::setSchedulerRunning(false);
422 void MainWindow::slotStopSelectedDownload()
424 m_dock->setDownloading(false);
426 foreach(TransferHandler * it, KGet::selectedTransfers())
427 it->stop();
430 void MainWindow::slotDeleteSelected()
432 foreach(TransferHandler * it, KGet::selectedTransfers())
434 it->stop();
435 m_viewsContainer->closeTransferDetails(it);
436 KGet::delTransfer(it);
440 void MainWindow::slotTransfersOpenDest()
442 QStringList openedDirs;
443 foreach(TransferHandler * it, KGet::selectedTransfers())
445 QString directory = it->dest().directory();
446 if( !openedDirs.contains( directory ) )
448 KToolInvocation::invokeBrowser( directory );
449 openedDirs.append( directory );
454 void MainWindow::slotTransfersShowDetails()
456 foreach(TransferHandler * it, KGet::selectedTransfers())
458 m_viewsContainer->showTransferDetails(it);
462 void MainWindow::slotTransfersCopySourceUrl()
464 foreach(TransferHandler * it, KGet::selectedTransfers())
466 QString sourceurl = it->source().url();
467 QClipboard *cb = QApplication::clipboard();
468 cb->setText(sourceurl, QClipboard::Selection);
469 cb->setText(sourceurl, QClipboard::Clipboard);
473 void MainWindow::slotConfigureNotifications()
475 KNotifyConfigWidget::configure(this);
478 void MainWindow::slotConfigureKeys()
480 KShortcutsDialog::configure(actionCollection());
483 void MainWindow::slotConfigureToolbars()
485 KEditToolBar edit( actionCollection() );
486 connect(&edit, SIGNAL( newToolbarConfig() ), this, SLOT( slotNewToolbarConfig() ));
487 edit.exec();
491 void MainWindow::slotSaveMyself()
493 // save last parameters ..
494 Settings::setMainPosition( pos() );
495 // .. and write config to disk
496 Settings::self()->writeConfig();
499 void MainWindow::slotNewToolbarConfig()
501 createGUI();
504 void MainWindow::slotNewConfig()
506 // Update here properties modified in the config dialog and not
507 // parsed often by the code. When clicking Ok or Apply of
508 // PreferencesDialog, this function is called.
510 m_viewsContainer->setExpandableDetails(Settings::showExpandableTransferDetails());
511 m_drop->setVisible(Settings::showDropTarget(), false);
512 m_dock->setVisible(Settings::enableSystemTray());
513 if(!Settings::enableSystemTray()) setVisible(true);
515 slotKonquerorIntegration(Settings::konquerorIntegration());
516 m_konquerorIntegration->setChecked(Settings::konquerorIntegration());
518 if (Settings::autoPaste())
519 clipboardTimer->start(1000);
520 else
521 clipboardTimer->stop();
522 m_autoPasteAction->setChecked(Settings::autoPaste());
525 void MainWindow::slotToggleAutoPaste()
527 bool autoPaste = !Settings::autoPaste();
528 Settings::setAutoPaste( autoPaste );
530 if (autoPaste)
531 clipboardTimer->start(1000);
532 else
533 clipboardTimer->stop();
534 m_autoPasteAction->setChecked(autoPaste);
537 void MainWindow::slotCheckClipboard()
539 QString clipData = QApplication::clipboard()->text( QClipboard::Clipboard ).trimmed();
541 if (clipData != lastClipboard)
543 lastClipboard = clipData;
544 if (lastClipboard.isEmpty())
545 return;
547 KUrl url = KUrl(lastClipboard);
549 if (url.isValid() && !url.isLocalFile())
550 KGet::addTransfer( url );
554 void MainWindow::slotTrayKonquerorIntegration(bool enable)
556 slotKonquerorIntegration(enable);
557 if (!enable && Settings::konquerorIntegration() && !Settings::expertMode())
559 KMessageBox::information(this,
560 i18n("KGet has been temporarily disabled as download manager for Konqueror. "
561 "If you want to disable it forever, go to Settings->Advanced and disable \"Use "
562 "as download manager for Konqueror\"."),
563 i18n("Konqueror Integration disabled"),
564 "KonquerorIntegrationDisabled");
568 void MainWindow::slotKonquerorIntegration(bool konquerorIntegration)
570 KConfig cfgKonqueror("konquerorrc", KConfig::NoGlobals);
571 cfgKonqueror.group("HTML Settings").writeEntry("DownloadManager",
572 QString(konquerorIntegration ? "kget" : QString()));
573 cfgKonqueror.sync();
576 void MainWindow::slotShowMenubar()
578 if(m_menubarAction->isChecked())
579 menuBar()->show();
580 else
581 menuBar()->hide();
584 /** widget events */
586 void MainWindow::closeEvent( QCloseEvent * e )
588 e->ignore();
589 if(!Settings::enableSystemTray()) {
590 slotQuit();
592 else {
593 hide();
597 void MainWindow::dragEnterEvent(QDragEnterEvent * event)
599 event->setAccepted(KUrl::List::canDecode(event->mimeData())
600 || event->mimeData()->hasText());
603 void MainWindow::dropEvent(QDropEvent * event)
605 KUrl::List list = KUrl::List::fromMimeData(event->mimeData());
606 QString str;
608 if (!list.isEmpty())
610 if (list.count() == 1)
612 str = event->mimeData()->text();
613 NewTransferDialog::showNewTransferDialog(str);
615 else
616 NewTransferDialog::showNewTransferDialog(list);
618 else
620 NewTransferDialog::showNewTransferDialog();
625 /** DBUS interface */
627 void MainWindow::addTransfers(const QString& src, const QString& dest, bool start)
629 // split src for the case it is a QStringList (e.g. from konqueror plugin)
630 KGet::addTransfer(src.split(";"), dest, QString(), start);
633 bool MainWindow::dropTargetVisible() const
635 return m_drop->isVisible();
638 void MainWindow::setDropTargetVisible( bool setVisible )
640 if ( setVisible != Settings::showDropTarget() )
641 m_drop->setVisible( setVisible );
644 void MainWindow::setOfflineMode( bool offline )
646 KGet::setSchedulerRunning(offline);
649 bool MainWindow::offlineMode() const
651 return !KGet::schedulerRunning();
654 QVariantMap MainWindow::transfers() const
656 return m_dbusModelObserver->transfers();
659 int MainWindow::transfersSpeed() const
661 return m_dbusModelObserver->transfersSpeed();
664 #include "mainwindow.moc"