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"
20 #include "conf/preferencesdialog.h"
21 #include "ui/viewscontainer.h"
23 #include "ui/droptarget.h"
24 #include "ui/groupseditdialog.h"
26 #include <kapplication.h>
27 #include <kstandarddirs.h>
28 #include <kmessagebox.h>
29 #include <kshortcutsdialog.h>
30 #include <kedittoolbar.h>
31 #include <knotifyconfigwidget.h>
32 #include <kfiledialog.h>
33 #include <ktoolinvocation.h>
35 #include <kiconloader.h>
36 #include <kstandardaction.h>
39 #include <kactionmenu.h>
44 MainWindow::MainWindow( QWidget
* parent
)
45 : KXmlGuiWindow( parent
),
48 resize(720, 380); // have a reasonable initial size, will be overwritten later if the user changes it
56 createGUI("kgetui.rc");
58 m_viewsContainer
= new ViewsContainer(this);
60 setCentralWidget(m_viewsContainer
);
62 // restore position, size and visibility
63 move( Settings::mainPosition() );
64 setAutoSaveSettings();
65 setPlainCaption(i18n("KGet"));
67 if ( Settings::showMain() )
72 //Some of the widgets are initialized in slotDelayedInit()
73 QTimer::singleShot( 0, this, SLOT(slotDelayedInit()) );
76 MainWindow::~MainWindow()
78 //Save the user's transfers
82 delete clipboardTimer
;
85 // reset konqueror integration (necessary if user enabled / disabled temporarily integration from tray)
86 slotKonquerorIntegration( Settings::konquerorIntegration() );
87 // the following call saves options set in above dtors
88 Settings::writeConfig();
92 void MainWindow::setupActions()
94 QAction
*newDownloadAction
= actionCollection()->addAction("new_download");
95 newDownloadAction
->setText(i18n("&New Download..."));
96 newDownloadAction
->setIcon(KIcon("document-new"));
97 newDownloadAction
->setShortcuts(KShortcut("Ctrl+N"));
98 connect(newDownloadAction
, SIGNAL(triggered()), SLOT(slotNewTransfer()));
100 QAction
*openAction
= actionCollection()->addAction("open");
101 openAction
->setText(i18n("&Open..."));
102 openAction
->setIcon(KIcon("document-open"));
103 openAction
->setShortcuts(KShortcut("Ctrl+O"));
104 connect(openAction
, SIGNAL(triggered()), SLOT(slotOpen()));
106 QAction
*exportAction
= actionCollection()->addAction("export_downloads");
107 exportAction
->setText(i18n("&Export Transfers List..."));
108 exportAction
->setIcon(KIcon("fileexport"));
109 exportAction
->setShortcuts(KShortcut("Ctrl+E"));
110 connect(exportAction
, SIGNAL(triggered()), SLOT(slotExportTransfers()));
112 QAction
*editGroupAction
= actionCollection()->addAction("edit_groups");
113 editGroupAction
->setText(i18n("Edit Groups..."));
114 editGroupAction
->setIcon(KIcon("todolist"));
115 connect(editGroupAction
, SIGNAL(triggered()), SLOT(slotEditGroups()));
117 m_autoPasteAction
= new KToggleAction(KIcon("klipper"),
118 i18n("Auto-Paste Mode"), actionCollection());
119 actionCollection()->addAction("auto_paste", m_autoPasteAction
);
120 m_autoPasteAction
->setChecked(Settings::autoPaste());
121 m_autoPasteAction
->setWhatsThis(i18n("<b>Auto paste</b> button toggles the auto-paste mode "
122 "on and off.\nWhen set, KGet will periodically scan "
123 "the clipboard for URLs and paste them automatically."));
124 connect(m_autoPasteAction
, SIGNAL(triggered()), SLOT(slotToggleAutoPaste()));
126 m_konquerorIntegration
= new KToggleAction(KIcon("konqueror"),
127 i18n("Use KGet as Konqueror Download Manager"), actionCollection());
128 actionCollection()->addAction("konqueror_integration", m_konquerorIntegration
);
129 connect(m_konquerorIntegration
, SIGNAL(triggered(bool)), SLOT(slotTrayKonquerorIntegration(bool)));
130 m_konquerorIntegration
->setChecked(Settings::konquerorIntegration());
132 // local - Destroys all sub-windows and exits
133 QAction
*quitAction
= KStandardAction::quit(this, SLOT(slotQuit()), actionCollection());
134 actionCollection()->addAction("quit", quitAction
);
135 // local - Standard configure actions
136 QAction
*preferencesAction
= KStandardAction::preferences(this, SLOT(slotPreferences()), actionCollection());
137 actionCollection()->addAction("preferences", preferencesAction
);
138 QAction
*configToolbarAction
= KStandardAction::configureToolbars(this, SLOT(slotConfigureToolbars()), actionCollection());
139 actionCollection()->addAction("configure_toolbars", configToolbarAction
);
140 QAction
*keyBindingsAction
= KStandardAction::keyBindings(this, SLOT(slotConfigureKeys()), actionCollection());
141 actionCollection()->addAction("configure_keys", keyBindingsAction
);
142 QAction
*cinfigNotifyAction
= KStandardAction::configureNotifications(this, SLOT(slotConfigureNotifications()), actionCollection());
143 actionCollection()->addAction("configure_notifications", cinfigNotifyAction
);
144 m_menubarAction
= KStandardAction::showMenubar(this, SLOT(slotShowMenubar()), actionCollection());
145 m_menubarAction
->setChecked(!menuBar()->isHidden());
146 actionCollection()->addAction("settings_showmenubar", m_menubarAction
);
148 // Transfer related actions
149 QAction
*deleteSelectedAction
= actionCollection()->addAction("delete_selected_download");
150 deleteSelectedAction
->setText(i18n("Delete Selected"));
151 deleteSelectedAction
->setIcon(KIcon("edit-delete"));
152 deleteSelectedAction
->setShortcuts(KShortcut("Del"));
153 connect(deleteSelectedAction
, SIGNAL(triggered()), SLOT(slotDeleteSelected()));
155 QAction
*startAllAction
= actionCollection()->addAction("start_all_download");
156 startAllAction
->setText(i18n("Start / Resume All"));
157 startAllAction
->setIcon(KIcon("media-seek-forward"));
158 connect(startAllAction
, SIGNAL(triggered()), SLOT(slotStartAllDownload()));
160 QAction
*startSelectedAction
= actionCollection()->addAction("start_selected_download");
161 startSelectedAction
->setText(i18n("Start / Resume Selected"));
162 startSelectedAction
->setIcon(KIcon("media-playback-start"));
163 connect(startSelectedAction
, SIGNAL(triggered()), SLOT(slotStartSelectedDownload()));
165 QAction
*stopAllAction
= actionCollection()->addAction("stop_all_download");
166 stopAllAction
->setText(i18n("Stop All"));
167 stopAllAction
->setIcon(KIcon("media-playback-pause"));
168 connect(stopAllAction
, SIGNAL(triggered()), SLOT(slotStopAllDownload()));
170 QAction
*stopSelectedAction
= actionCollection()->addAction("stop_selected_download");
171 stopSelectedAction
->setText(i18n("Stop Selected"));
172 stopSelectedAction
->setIcon(KIcon("media-playback-pause"));
173 connect(stopSelectedAction
, SIGNAL(triggered()), SLOT(slotStopSelectedDownload()));
175 KActionMenu
*startActionMenu
= new KActionMenu(KIcon("media-playback-start"), i18n("Start / Resume"),
177 actionCollection()->addAction("start_menu", startActionMenu
);
178 startActionMenu
->setDelayed(true);
179 startActionMenu
->addAction(startAllAction
);
180 startActionMenu
->addAction(startSelectedAction
);
181 connect(startActionMenu
, SIGNAL(triggered()), SLOT(slotStartDownload()));
183 KActionMenu
*stopActionMenu
= new KActionMenu(KIcon("media-playback-pause"), i18n("Stop"),
185 actionCollection()->addAction("stop_menu", stopActionMenu
);
186 stopActionMenu
->setDelayed(true);
187 stopActionMenu
->addAction(stopAllAction
);
188 stopActionMenu
->addAction(stopSelectedAction
);
189 connect(stopActionMenu
, SIGNAL(triggered()), SLOT(slotStopDownload()));
191 QAction
*openDestAction
= actionCollection()->addAction("transfer_open_dest");
192 openDestAction
->setText(i18n("Open Destination"));
193 openDestAction
->setIcon(KIcon("folder"));
194 connect(openDestAction
, SIGNAL(triggered()), SLOT(slotTransfersOpenDest()));
196 QAction
*showDetailsAction
= actionCollection()->addAction("transfer_show_details");
197 showDetailsAction
->setText(i18n("Show Details"));
198 showDetailsAction
->setIcon(KIcon("configure"));
199 connect(showDetailsAction
, SIGNAL(triggered()), SLOT(slotTransfersShowDetails()));
201 QAction
*copyUrlAction
= actionCollection()->addAction("transfer_copy_source_url");
202 copyUrlAction
->setText(i18n("Copy URL to Clipboard"));
203 copyUrlAction
->setIcon(KIcon("klipper"));
204 connect(copyUrlAction
, SIGNAL(triggered()), SLOT(slotTransfersCopySourceUrl()));
207 void MainWindow::slotDelayedInit()
209 //Here we import the user's transfers.
210 KGet::load( KStandardDirs::locateLocal("appdata", "transfers.kgt") );
213 m_drop
= new DropTarget(this);
215 if ( Settings::showDropTarget() || Settings::firstRun() )
216 m_drop
->setVisible(true);
219 m_dock
= new Tray(this);
223 setAcceptDrops(true);
225 // enable hide toolbar
226 setStandardToolBarMenuEnabled(true);
228 // session management stuff
229 connect(kapp
, SIGNAL(saveYourself()), SLOT(slotSaveMyself()));
231 // set auto-resume in kioslaverc (is there a cleaner way?)
232 KConfig
cfg("kioslaverc", KConfig::NoGlobals
);
233 cfg
.group(QString()).writeEntry("AutoResume", true);
236 // immediately start downloading if configured this way
237 if ( Settings::downloadAtStartup() )
240 if (Settings::firstRun()) {
241 if (KMessageBox::questionYesNoCancel(this ,i18n("This is the first time you have run KGet.\n"
242 "Would you like to use KGet as the download manager for Konqueror?"),
243 i18n("Konqueror Integration"), KGuiItem(i18n("Enable")),
244 KGuiItem(i18n("Do Not Enable")))
245 == KMessageBox::Yes
) {
246 Settings::setKonquerorIntegration(true);
247 slotKonquerorIntegration(true);
250 // reset the FirstRun config option
251 Settings::setFirstRun(false);
255 lastClipboard
= QApplication::clipboard()->text( QClipboard::Clipboard
).trimmed();
256 clipboardTimer
= new QTimer(this);
257 connect(clipboardTimer
, SIGNAL(timeout()), SLOT(slotCheckClipboard()));
258 if ( Settings::autoPaste() )
259 clipboardTimer
->start(1000);
262 void MainWindow::slotNewTransfer()
264 KGet::addTransfer(KUrl());
267 void MainWindow::slotOpen()
269 QString filename
= KFileDialog::getOpenFileName
271 "*.kgt *.torrent *.metalink|" + i18n("All Openable Files") + " (*.kgt *.torrent *.metalink)",
276 if(filename
.endsWith(".kgt"))
278 KGet::load(filename
);
282 if(filename
.endsWith(".metalink"))
284 KGet::addMetaLink( KUrl( filename
) );
288 if(!filename
.isEmpty())
289 KGet::addTransfer( KUrl( filename
) );
292 void MainWindow::slotQuit()
294 // if (m_scheduler->countRunningJobs() > 0)
296 // if (KMessageBox::warningYesNo(this,
297 // i18n("Some transfers are still running.\n"
298 // "Are you sure you want to close KGet?"),
299 // i18n("Warning"), KStdGuiItem::yes(), KStdGuiItem::no(),
300 // "ExitWithActiveTransfers") == KMessageBox::No)
304 Settings::writeConfig();
308 void MainWindow::slotPreferences()
310 // an instance the dialog could be already created and could be cached,
311 // in which case you want to display the cached dialog
312 if ( PreferencesDialog::showDialog( "preferences" ) )
315 // we didn't find an instance of this dialog, so lets create it
316 PreferencesDialog
* dialog
= new PreferencesDialog( this, Settings::self() );
317 KGet::setPluginsSettingsWidget( dialog
->pluginsWidget() );
318 // keep us informed when the user changes settings
319 connect( dialog
, SIGNAL(settingsChanged(const QString
&)),
320 this, SLOT(slotNewConfig()) );
325 void MainWindow::slotExportTransfers()
327 QString filename
= KFileDialog::getSaveFileName
329 "*.kgt|" + i18n("KGet Transfer List") + " (*.kgt)",
331 i18n("Export Transfers")
334 if(!filename
.isEmpty())
335 KGet::save(filename
);
338 void MainWindow::slotEditGroups()
340 GroupsEditDialog
dialog(this);
345 void MainWindow::slotStartDownload()
347 if(KGet::selectedTransfers().size() == 0)
348 slotStartAllDownload();
350 slotStartSelectedDownload();
353 void MainWindow::slotStartAllDownload()
355 m_dock
->setDownloading(true);
357 KGet::setSchedulerRunning(true);
360 void MainWindow::slotStartSelectedDownload()
362 m_dock
->setDownloading(true);
364 foreach(TransferHandler
* it
, KGet::selectedTransfers())
368 void MainWindow::slotStopDownload()
370 if(KGet::selectedTransfers().size() == 0)
371 slotStopAllDownload();
373 slotStopSelectedDownload();
376 void MainWindow::slotStopAllDownload()
378 m_dock
->setDownloading(false);
380 KGet::setSchedulerRunning(false);
383 void MainWindow::slotStopSelectedDownload()
385 m_dock
->setDownloading(false);
387 foreach(TransferHandler
* it
, KGet::selectedTransfers())
391 void MainWindow::slotDeleteSelected()
393 foreach(TransferHandler
* it
, KGet::selectedTransfers())
396 KGet::delTransfer(it
);
400 void MainWindow::slotTransfersOpenDest()
402 QStringList openedDirs
;
403 foreach(TransferHandler
* it
, KGet::selectedTransfers())
405 QString directory
= it
->dest().directory();
406 if( !openedDirs
.contains( directory
) )
408 KToolInvocation::invokeBrowser( directory
);
409 openedDirs
.append( directory
);
414 void MainWindow::slotTransfersShowDetails()
416 foreach(TransferHandler
* it
, KGet::selectedTransfers())
418 m_viewsContainer
->showTransferDetails(it
);
422 void MainWindow::slotTransfersCopySourceUrl()
424 foreach(TransferHandler
* it
, KGet::selectedTransfers())
426 QString sourceurl
= it
->source().url();
427 QClipboard
*cb
= QApplication::clipboard();
428 cb
->setText(sourceurl
, QClipboard::Selection
);
429 cb
->setText(sourceurl
, QClipboard::Clipboard
);
433 void MainWindow::slotConfigureNotifications()
435 KNotifyConfigWidget::configure(this);
438 void MainWindow::slotConfigureKeys()
440 KShortcutsDialog::configure(actionCollection());
443 void MainWindow::slotConfigureToolbars()
445 KEditToolBar
edit( actionCollection() );
446 connect(&edit
, SIGNAL( newToolbarConfig() ), this, SLOT( slotNewToolbarConfig() ));
451 void MainWindow::slotSaveMyself()
453 // save last parameters ..
454 Settings::setMainPosition( pos() );
455 // .. and write config to disk
456 Settings::writeConfig();
459 void MainWindow::slotNewToolbarConfig()
464 void MainWindow::slotNewConfig()
466 // Update here properties modified in the config dialog and not
467 // parsed often by the code. When clicking Ok or Apply of
468 // PreferencesDialog, this function is called.
470 m_drop
->setVisible(Settings::showDropTarget(), false);
472 slotKonquerorIntegration(Settings::konquerorIntegration());
473 m_konquerorIntegration
->setChecked(Settings::konquerorIntegration());
475 if (Settings::autoPaste())
476 clipboardTimer
->start(1000);
478 clipboardTimer
->stop();
479 m_autoPasteAction
->setChecked(Settings::autoPaste());
482 void MainWindow::slotToggleAutoPaste()
484 bool autoPaste
= !Settings::autoPaste();
485 Settings::setAutoPaste( autoPaste
);
488 clipboardTimer
->start(1000);
490 clipboardTimer
->stop();
491 m_autoPasteAction
->setChecked(autoPaste
);
494 void MainWindow::slotCheckClipboard()
496 QString clipData
= QApplication::clipboard()->text( QClipboard::Clipboard
).trimmed();
498 if (clipData
!= lastClipboard
)
500 lastClipboard
= clipData
;
501 if (lastClipboard
.isEmpty())
504 KUrl url
= KUrl(lastClipboard
);
506 if (url
.isValid() && !url
.isLocalFile())
507 KGet::addTransfer( url
);
511 void MainWindow::slotTrayKonquerorIntegration(bool enable
)
513 slotKonquerorIntegration(enable
);
514 if (!enable
&& Settings::konquerorIntegration() && !Settings::expertMode())
516 KMessageBox::information(this,
517 i18n("KGet has been temporarily disabled as download manager for Konqueror. "
518 "If you want to disable it forever, go to Settings->Advanced and disable \"Use "
519 "as download manager for Konqueror\"."),
520 i18n("Konqueror Integration disabled"),
521 "KonquerorIntegrationDisabled");
525 void MainWindow::slotKonquerorIntegration(bool konquerorIntegration
)
527 KConfig
cfgKonqueror("konquerorrc", KConfig::NoGlobals
);
528 cfgKonqueror
.group("HTML Settings").writeEntry("DownloadManager",
529 QString(konquerorIntegration
? "kget" : QString()));
533 void MainWindow::slotShowMenubar()
535 if(m_menubarAction
->isChecked())
543 void MainWindow::closeEvent( QCloseEvent
* e
)
549 void MainWindow::dragEnterEvent(QDragEnterEvent
* event
)
551 event
->setAccepted(KUrl::List::canDecode(event
->mimeData())
552 || event
->mimeData()->hasText());
555 void MainWindow::dropEvent(QDropEvent
* event
)
557 KUrl::List list
= KUrl::List::fromMimeData(event
->mimeData());
561 KGet::addTransfer(list
);
564 str
= event
->mimeData()->text();
565 KGet::addTransfer(KUrl(str
));
570 /** DBUS interface */
572 void MainWindow::addTransfers(const QString
& src
, const QString
& dest
)
574 // split src for the case it is a QStringList (e.g. from konqueror plugin)
575 KGet::addTransfer(src
.split(";"), dest
);
578 bool MainWindow::dropTargetVisible() const
580 return m_drop
->isVisible();
583 void MainWindow::setDropTargetVisible( bool setVisible
)
585 if ( setVisible
!= Settings::showDropTarget() )
586 m_drop
->setVisible( setVisible
);
589 void MainWindow::setOfflineMode( bool offline
)
591 KGet::setSchedulerRunning(offline
);
594 bool MainWindow::offlineMode() const
596 return !KGet::schedulerRunning();
599 #include "mainwindow.moc"