• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • ui
newtransferdialog.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4  Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
5  Copyright (C) 2008 - 2009 by Lukas Appelhans <l.appelhans@gmx.de>
6  Copyright (C) 2010 by Matthias Fuchs <mat69@gmx.net>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 */
13 
14 #include "newtransferdialog.h"
15 
16 #include "core/filedeleter.h"
17 #include "core/kget.h"
18 #include "mainwindow.h"
19 #include "core/mostlocalurl.h"
20 #include "core/transfertreemodel.h"
21 #include "core/transfergrouphandler.h"
22 #include "core/plugin/transferfactory.h"
23 #include "core/urlchecker.h"
24 #include "settings.h"
25 
26 #include <QApplication>
27 #include <QClipboard>
28 #include <QDir>
29 #include <QFileInfo>
30 #include <QtCore/QTimer>
31 
32 #include <KLocale>
33 #include <QListWidgetItem>
34 #include <KColorScheme>
35 #include <KDebug>
36 #include <KFileDialog>
37 #include <KWindowSystem>
38 
39 K_GLOBAL_STATIC(NewTransferDialogHandler, newTransferDialogHandler)
40 
41 
42 NewTransferDialog::NewTransferDialog(QWidget *parent)
43  : KDialog(parent),
44  m_window(0),
45  m_existingTransfer(0),
46  m_multiple(false),
47  m_overWriteSingle(false)
48 {
49  setModal(true);
50  setCaption(i18n("New Download"));
51  showButtonSeparator(true);
52 
53  QWidget *widget = new QWidget(this);
54  ui.setupUi(widget);
55  setMainWidget(widget);
56 
57  enableButtonOk(false);
58 
59  //timer to avoid constant checking of the input
60  m_timer = new QTimer(this);
61  m_timer->setInterval(350);
62  m_timer->setSingleShot(true);
63  connect(m_timer, SIGNAL(timeout()), this, SLOT(checkInput()));
64 
65  const KColorScheme scheme = KColorScheme(QPalette::Active, KColorScheme::View);
66  m_existingFileBackground = scheme.background(KColorScheme::NeutralBackground);
67  m_normalBackground = scheme.background();
68 
69 
70  // properties of the m_destRequester combobox
71  ui.destRequester->comboBox()->setDuplicatesEnabled(false);
72  ui.destRequester->comboBox()->setUrlDropsEnabled(true);
73  ui.destRequester->comboBox()->setEditable(true);
74  ui.destRequester->fileDialog()->setKeepLocation(true);
75 
76  ui.errorWidget->setCloseButtonVisible(false);
77 
78  connect(ui.groupComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setDefaultDestination()));
79 
80  connect(ui.urlRequester, SIGNAL(textChanged(QString)), this, SLOT(setDefaultDestination()));
81  connect(ui.destRequester, SIGNAL(textChanged(QString)), this, SLOT(inputTimer()));
82  connect(ui.urlRequester, SIGNAL(textChanged(QString)), this, SLOT(inputTimer()));
83  connect(ui.listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(inputTimer()));
84  connect(this, SIGNAL(finished(int)), this, SLOT(slotFinished(int)));
85 }
86 
87 NewTransferDialog::~NewTransferDialog()
88 {
89 }
90 
91 void NewTransferDialog::setMultiple(bool useMultiple)
92 {
93  m_multiple = useMultiple;
94 
95  const Qt::Alignment alignment = Qt::AlignLeft | (m_multiple ? Qt::AlignTop : Qt::AlignVCenter);
96  ui.urlLabel->setAlignment(alignment);
97  ui.urlRequester->setVisible(!m_multiple);
98  ui.listWidget->setVisible(m_multiple);
99  ui.destRequester->setMode(m_multiple ? KFile::Directory : KFile::File);
100 }
101 
102 void NewTransferDialog::clear()
103 {
104  ui.urlRequester->clear();
105  ui.urlRequester->setFocus();
106  ui.listWidget->clear();
107  ui.destRequester->comboBox()->clear();
108  ui.destRequester->clear();
109  m_destination.clear();
110  m_sources.clear();
111  m_existingTransfer = 0;
112  m_overWriteSingle = false;
113 
114  //add all destinations
115  QStringList list;
116  QString downloadPath = KGet::generalDestDir();
117  if (!downloadPath.isEmpty()) {
118  if (!downloadPath.endsWith('/')) {
119  downloadPath.append('/');
120  }
121  list << downloadPath;
122  }
123  foreach (TransferGroupHandler *handler, KGet::allTransferGroups()) {
124  const QString folder = handler->defaultFolder();
125  if (!folder.isEmpty()) {
126  list << (folder.endsWith('/') ? folder : folder + '/');
127  }
128  }
129 
130  list.removeDuplicates();
131  ui.destRequester->comboBox()->insertItems(0, list);
132 
133  //add all transfer groups
134  ui.groupComboBox->clear();
135  foreach (TransferGroupHandler *group, KGet::allTransferGroups()) {
136  ui.groupComboBox->addItem(KIcon(group->iconName()), group->name());
137  }
138  ui.groupComboBox->setCurrentItem(Settings::lastGroup());
139  if (ui.groupComboBox->currentIndex() == -1) {
140  ui.groupComboBox->setCurrentIndex(0);
141  }
142 
143  const bool multipleGroups = KGet::transferGroupNames().count();
144  ui.groupComboBox->setVisible(multipleGroups);
145  ui.groupLabel->setVisible(multipleGroups);
146 }
147 
148 void NewTransferDialog::setSource(const KUrl::List &sources)
149 {
150  if (sources.isEmpty()) {
151  return;
152  }
153 
154  if (sources.count() == 1) {
155  KUrl m_srcUrl = sources.first().url();
156  ui.urlRequester->clear();
157  if (m_srcUrl.isEmpty()) {
158  m_srcUrl = KUrl(QApplication::clipboard()->text(QClipboard::Clipboard).trimmed());
159  }
160 
161  if (UrlChecker::checkSource(m_srcUrl) == UrlChecker::NoError) {
162  ui.urlRequester->insert(m_srcUrl.prettyUrl());
163  }
164  } else {
165  foreach (const KUrl &sourceUrl, sources) {
166  if (sourceUrl.url() != KUrl(sourceUrl.url()).fileName()) {//TODO simplify, whatfor is this check anyway, shouldn't the sources be checked already and if not add this to UrlChecker
167  kDebug(5001) << "Insert" << sourceUrl;
168  QListWidgetItem *newItem = new QListWidgetItem(sourceUrl.pathOrUrl(), ui.listWidget);
169  newItem->setCheckState(Qt::Checked);
170  }
171  }
172  }
173 
174  const QList<TransferGroupHandler*> groups = KGet::groupsFromExceptions(sources.first());
175  if (!groups.isEmpty()) {
176  ui.groupComboBox->setCurrentIndex(ui.groupComboBox->findText(groups.first()->name()));
177  }
178 }
179 
180 void NewTransferDialog::setDestinationFileName(const QString &filename)
181 {
182  ui.destRequester->setUrl(QString(ui.destRequester->url().path(KUrl::AddTrailingSlash) + filename));
183 }
184 
185 void NewTransferDialog::setDestination()
186 {
187  //sets destRequester to either display the defaultFolder of group or the generalDestDir
188  QString group = ui.groupComboBox->currentText();
189  TransferGroupHandler * current = 0;
190  foreach (TransferGroupHandler * handler, KGet::allTransferGroups()) {
191  if (handler->name() == group) {
192  current = handler;
193  break;
194  }
195  }
196 
197  if (current) {
198  QString groupFolder = current->defaultFolder();
199  if (groupFolder.isEmpty()) {
200  groupFolder = KGet::generalDestDir();
201  }
202  if (!groupFolder.endsWith('/')) {
203  groupFolder.append('/');
204  }
205  ui.destRequester->comboBox()->setCurrentItem(groupFolder, true);
206  }
207 }
208 
209 void NewTransferDialog::showDialog(KUrl::List list, const QString &suggestedFileName)
210 {
211  //TODO handle the case where for some there are suggested file names --> own file name column in multiple setting
212  //the dialog is already in use, adapt it
213  if (isVisible()) {
214  list << m_sources;
215  }
216  clear();//Let's clear the old stuff
217  m_sources << list;
218  UrlChecker::removeDuplicates(m_sources);
219  const int size = m_sources.size();
220  kDebug(5001) << "SET SOURCES " << m_sources << " MULTIPLE " << (size > 1);
221  setMultiple(size > 1);
222 
223  if (size) {
224  if (size == 1 && !suggestedFileName.isEmpty()) {
225  setDestinationFileName(suggestedFileName);
226  }
227 
228  setSource(m_sources);
229  }
230 
231  prepareDialog();
232 }
233 
234 void NewTransferDialog::setDefaultDestination()
235 {
236  //NOTE if the user enters a file name manually and the changes the group the manually entered file name will be overwritten
237  setDestination();
238 
239  //set a file name
240  if (!m_multiple) {
241  const KUrl url(ui.urlRequester->text().trimmed());
242  if ((UrlChecker::checkSource(url) == UrlChecker::NoError) &&
243  QFileInfo(ui.destRequester->url().toLocalFile()).isDir()) {
244  setDestinationFileName(url.fileName());
245  }
246  }
247 }
248 
249 void NewTransferDialog::prepareDialog()
250 {
251  if (m_window) {
252  KWindowInfo info = KWindowSystem::windowInfo(m_window->winId(), NET::WMDesktop, NET::WMDesktop);
253  KWindowSystem::setCurrentDesktop(info.desktop());
254  KWindowSystem::forceActiveWindow(m_window->winId());
255  }
256 
257  kDebug(5001) << "Show the dialog!";
258  show();
259 }
260 
261 bool NewTransferDialog::isEmpty()
262 {
263  return (m_multiple ? !ui.listWidget->count() : ui.urlRequester->text().trimmed().isEmpty());
264 }
265 
266 void NewTransferDialog::inputTimer()
267 {
268  enableButtonOk(false);
269  m_timer->start();
270 }
271 
272 void NewTransferDialog::checkInput()
273 {
274  KUrl source = KUrl(ui.urlRequester->text().trimmed());
275  const KUrl dest = ui.destRequester->url();
276 
277  //check the destination folder
278  UrlChecker::UrlError error = UrlChecker::checkFolder(dest);
279  const bool folderValid = (error == UrlChecker::NoError);
280  bool destinationValid = false;
281  QString infoText;
282  QString warningText;
283  if (!folderValid) {
284  if (m_multiple) {
285  infoText = UrlChecker::message(KUrl(), UrlChecker::Folder, error);
286  } else {
287  //might be a destination instead of a folder
288  destinationValid = (UrlChecker::checkDestination(dest) == UrlChecker::NoError);
289  }
290  } else {
291  m_destination = dest;
292  }
293 
294  //check the source
295  if (!m_multiple) {
296  source = mostLocalUrl(source);
297  }
298  error = UrlChecker::checkSource(source);
299  const bool sourceValid = (error == UrlChecker::NoError);
300  if (!m_multiple && !sourceValid) {
301  infoText = UrlChecker::message(KUrl(), UrlChecker::Source, error);
302  }
303 
304  //check if any sources are checked and for existing transfers or destinations
305  bool filesChecked = false;
306  if (m_multiple && folderValid) {
307  KListWidget *list = ui.listWidget;
308 
309  //check if some sources have been checked
310  for (int i = 0; i < list->count(); ++i) {
311  QListWidgetItem *item = list->item(i);
312  if (item->checkState() == Qt::Checked) {
313  filesChecked = true;
314  break;
315  }
316  }
317  if (!filesChecked) {
318  infoText = i18n("Select at least one source url.");
319  }
320 
321  //check if there are existing files
322  if (filesChecked) {
323  bool existingFile = false;
324  for (int i = 0; i < list->count(); ++i) {
325  QListWidgetItem *item = list->item(i);
326  const KUrl source = KUrl(item->text());
327  const KUrl destUrl = UrlChecker::destUrl(dest, source);
328  if (UrlChecker::wouldOverwrite(source, destUrl)) {
329  item->setBackground(m_existingFileBackground);
330  existingFile = true;
331  } else {
332  item->setBackground(m_normalBackground);
333  }
334  }
335  if (existingFile) {
336  warningText = i18n("Files that exist already in the current folder have been marked.");//TODO better message
337  }
338  }
339  }
340 
341  //single file
342  UrlChecker::UrlWarning warning = UrlChecker::NoWarning;
343  if (!m_multiple && sourceValid && (folderValid || destinationValid)) {
344  m_destination = UrlChecker::destUrl(dest, source);
345  //show only one message for existing transfers
346  m_existingTransfer = UrlChecker::existingTransfer(source, UrlChecker::Source, &warning);
347  if (m_existingTransfer) {
348  warningText = UrlChecker::message(KUrl(), UrlChecker::Source, warning);
349  } else {
350  m_existingTransfer = UrlChecker::existingTransfer(m_destination, UrlChecker::Destination, &warning);
351  if (m_existingTransfer) {
352  warningText = UrlChecker::message(KUrl(), UrlChecker::Destination, warning);
353  }
354  }
355 
356  if (UrlChecker::wouldOverwrite(KUrl(ui.urlRequester->text().trimmed()), m_destination)) {
357  m_overWriteSingle = true;
358  if (!warningText.isEmpty()) {
359  warningText += '\n';
360  }
361  warningText += UrlChecker::message(KUrl(), UrlChecker::Destination, UrlChecker::ExistingFile);
362  } else {
363  m_overWriteSingle = false;
364  }
365  }
366 
367  if (!infoText.isEmpty()) {
368  setInformation(infoText);
369  } else if (!warningText.isEmpty()) {
370  setWarning(warningText);
371  } else {
372  ui.errorWidget->hide();
373  }
374 
375  //activate the ok button
376  if (m_multiple) {
377  enableButtonOk(folderValid && filesChecked);
378  } else {
379  enableButtonOk((folderValid || destinationValid) && sourceValid);
380  }
381 
382  kDebug(5001) << source << source.fileName() << dest << dest.fileName();
383 }
384 
385 void NewTransferDialog::slotFinished(int resultCode)
386 {
387  if (resultCode == KDialog::Accepted) {
388  dialogAccepted();
389  }
390  clear();
391 }
392 
393 void NewTransferDialog::dialogAccepted()
394 {
395  kDebug(5001) << "Dialog accepted.";
396 
397  //an existing transfer has been specified and since ok was clicked, it was chosen to be overwritten
398  if (m_existingTransfer) {
399  kDebug(5001) << "Removing existing transfer:" << m_existingTransfer;
400  KGet::delTransfer(m_existingTransfer);
401  }
402 
403  //set the last directory
404  QString dir = m_destination.toLocalFile();
405  if (!QFileInfo(dir).isDir()) {
406  dir = m_destination.directory();
407  }
408  Settings::setLastDirectory(dir);
409  Settings::self()->writeConfig();
410 
411  const QString group = ui.groupComboBox->currentText();
412 
414  QList<KGet::TransferData> data;
415  if (!m_multiple) {
416  if (m_overWriteSingle) {
417  kDebug(5001) << "Removing existing file:" << m_destination;
418  //removes m_destination if it exists, do that here so that it is removed no matter if a transfer could be created or not
419  //as the user decided to throw the file away
420  FileDeleter::deleteFile(m_destination);
421  }
422 
423  //sourceUrl is valid, has been checked before
424  const KUrl sourceUrl = KUrl(ui.urlRequester->text().trimmed());
425  kDebug(5001) << "Downloading" << sourceUrl << "to" << m_destination;
426  data << KGet::TransferData(sourceUrl, m_destination, group);
427  } else {
428  KUrl::List list;
429  for (int i = 0; i != ui.listWidget->count(); ++i) {
430  QListWidgetItem *item = ui.listWidget->item(i);
431 
432  //find selected sources
433  if (item->checkState() == Qt::Checked) {
434  //both sourceUrl and destUrl are valid, they have been tested in checkInput
435  const KUrl sourceUrl = KUrl(item->text().trimmed());
436  const KUrl destUrl = UrlChecker::destUrl(m_destination, sourceUrl);
437  kDebug(5001) << "Downloading" << sourceUrl << "to" << destUrl;
438 
439  //file exists already, remove it
440  if (item->background() == m_existingFileBackground) {
441  kDebug(5001) << "Removing existing file:" << destUrl;
442  //removes destUrl if it exists, do that here so that it is removed no matter if a transfer could be created or not
443  //as the user decided to throw the file away
444  FileDeleter::deleteFile(destUrl);
445  }
446 
447  data << KGet::TransferData(sourceUrl, destUrl, group);
448  }
449  }
450  }
451 
452  if (!data.isEmpty()) {
453  Settings::setLastGroup(ui.groupComboBox->currentText());
454  KGet::createTransfers(data);
455  }
456 }
457 
458 void NewTransferDialog::setInformation(const QString &information)
459 {
460  ui.errorWidget->setMessageType(KMessageWidget::Information);
461  ui.errorWidget->setText(information);
462  ui.errorWidget->setVisible(!information.isEmpty());
463 }
464 
465 void NewTransferDialog::setWarning(const QString &warning)
466 {
467  ui.errorWidget->setMessageType(KMessageWidget::Warning);
468  ui.errorWidget->setText(warning);
469  ui.errorWidget->setVisible(!warning.isEmpty());
470 }
471 
472 
473 
482 NewTransferDialogHandler::NewTransferDialogHandler(QObject *parent)
483  : QObject(parent),
484  m_nextJobId(0)
485 {
486 }
487 
488 NewTransferDialogHandler::~NewTransferDialogHandler()
489 {
490 }
491 
492 
493 void NewTransferDialogHandler::showNewTransferDialog(const KUrl &url)
494 {
495  showNewTransferDialog(url.isEmpty() ? KUrl::List() : KUrl::List() << url);
496 }
497 
498 void NewTransferDialogHandler::showNewTransferDialog(KUrl::List urls)
499 {
500  if (urls.isEmpty()) {
501  newTransferDialogHandler->createDialog(urls, QString());
502  return;
503  }
504 
505  QHash<int, UrlData>::iterator itUrls = newTransferDialogHandler->m_urls.insert(newTransferDialogHandler->m_nextJobId, UrlData());
506  QString folder;
507  QString suggestedFileName;
508 
510  if (urls.count() == 2) {
511  const KUrl lastUrl = urls.last();
512  QDir dir(lastUrl.toLocalFile());
513 
514  //check if last url is a file path, either absolute or relative
515  if (lastUrl.isLocalFile()) {
516  if (QDir::isAbsolutePath(lastUrl.toLocalFile())) {
517  if (dir.exists()) {
518  //second url is a folder path
519  folder = lastUrl.path(KUrl::AddTrailingSlash);
520  } else {
521  //second url is a file path, use this one
522  folder = lastUrl.directory(KUrl::AppendTrailingSlash);
523  suggestedFileName = lastUrl.fileName();
524  }
525  urls.removeLast();
526  } else {
527  //second url is just a file name
528  suggestedFileName = lastUrl.fileName(KUrl::ObeyTrailingSlash);
529  urls.removeLast();
530  }
531  } else if (!lastUrl.isValid() || (lastUrl.scheme().isEmpty() && lastUrl.directory().isEmpty())) {
532  // Sometimes valid filenames are not recognised by KURL::isLocalFile(), they are marked as invalid then
533  suggestedFileName = lastUrl.url();
534  urls.removeLast();
535  }
536  }
537 
539  if (urls.count() > 2 && urls.last().isLocalFile()) {
540 
546 // if (!QFileInfo(urls.last().toLocalFile()).isDir()) {
547 // folder = urls.last().directory(KUrl::AppendTrailingSlash);
548 // } else {
549  folder = urls.last().path(KUrl::AddTrailingSlash);//checks if that folder is correct happen later
550 // }
551  urls.removeLast();
552  }
553 
554  //add a folder or suggestedFileName if they are valid
555  if (!folder.isEmpty() && KGet::isValidDestDirectory(folder)) {
556  (*itUrls).folder = folder;
557  }
558  if (!suggestedFileName.isEmpty()) {
559  (*itUrls).suggestedFileName = KUrl(suggestedFileName).pathOrUrl();//pathOrUrl to get a non percent encoded url
560  }
561 
562  newTransferDialogHandler->m_numJobs[newTransferDialogHandler->m_nextJobId] = urls.count();
563  foreach (const KUrl &url, urls) {
564  //needed to avoid when protocols like the desktop protocol is used, see bko:185283
565  KIO::Job *job = mostLocalUrlJob(url);
566  job->setProperty("jobId", (newTransferDialogHandler->m_nextJobId));
567  connect(job, SIGNAL(result(KJob*)), newTransferDialogHandler, SLOT(slotMostLocalUrlResult(KJob*)));
568  job->start();
569  }
570 
571  ++(newTransferDialogHandler->m_nextJobId);
572 }
573 
574 void NewTransferDialogHandler::slotMostLocalUrlResult(KJob *j)
575 {
576  MostLocalUrlJob *job = static_cast<MostLocalUrlJob*>(j);
577  const int jobId = job->property("jobId").toInt();
578 
579  if (job->error()) {
580  kWarning(5001) << "An error happened for" << job->url();
581  } else {
582  m_urls[jobId].urls << job->mostLocalUrl();
583  }
584  --m_numJobs[jobId];
585 
586  if (m_numJobs[jobId] <= 0) {
587  handleUrls(jobId);
588  }
589 }
590 
591 void NewTransferDialogHandler::handleUrls(const int jobId)
592 {
593  QHash<int, UrlData>::iterator itUrls = m_urls.find(jobId);
594  if (itUrls == m_urls.end()) {
595  kWarning(5001) << "JobId" << jobId << "was not defined, could not handle urls for it.";
596  return;
597  }
598 
599  KUrl::List urls = (*itUrls).urls;
600  UrlChecker::removeDuplicates(urls);
601 
602  QString folder = (*itUrls).folder;
603  if (!folder.isEmpty() && (UrlChecker::checkFolder(KUrl(folder), true) != UrlChecker::NoError)) {
604  folder.clear();
605  }
606 
607  const QString suggestedFileName = (*itUrls).suggestedFileName;
608  KUrl newDest;
609  const KUrl folderUrl = KUrl(folder);
610 
611  //check if the sources are correct
612  UrlChecker check(UrlChecker::Source);
613  check.addUrls(urls);
614  check.displayErrorMessages();
615  check.existingTransfers();
616  urls = check.correctUrls();
617 
618  QList<KGet::TransferData> data;
619 
621  if (!suggestedFileName.isEmpty() && (urls.count() == 1)) {
622  const KUrl sourceUrl = urls.first();
623  const QList<TransferGroupHandler*> groups = KGet::groupsFromExceptions(sourceUrl);
624  const QString groupName = (groups.isEmpty() ? QString() : groups.first()->name());
625  QString defaultFolder;
626  if (groups.isEmpty()) {
627  defaultFolder = (Settings::askForDestination() ? QString() : KGlobalSettings::downloadPath());
628  } else {
629  defaultFolder = groups.first()->defaultFolder();
630  }
631 
632  if (!folder.isEmpty()) {
633  const KUrl destUrl = UrlChecker::destUrl(KUrl(folder), sourceUrl, suggestedFileName);
634  newDest = check.checkExistingFile(sourceUrl, destUrl);
635  if (!newDest.isEmpty()) {
636  data << KGet::TransferData(sourceUrl, newDest, groupName);
637  }
638  urls.removeFirst();
639  } else if (((!groups.isEmpty() && !Settings::directoriesAsSuggestion()) || !Settings::askForDestination()) &&
640  (UrlChecker::checkFolder(KUrl(defaultFolder)) == UrlChecker::NoError)) {
641  const KUrl destUrl = UrlChecker::destUrl(KUrl(defaultFolder), sourceUrl, suggestedFileName);
642  newDest = check.checkExistingFile(sourceUrl, destUrl);
643  if (!newDest.isEmpty()) {
644  data << KGet::TransferData(sourceUrl, newDest, groupName);
645  }
646  urls.removeFirst();
647  }
648  }
649 
651  if (!folder.isEmpty()) {
652  //find the associated groups first, we just need the first matching group though
653  const QList<TransferGroupHandler*> groups = KGet::allTransferGroups();
654  foreach (TransferGroupHandler *group, groups) {
655  if (urls.isEmpty()) {
656  break;
657  }
658 
659  const QString groupName = group->name();
660  const QStringList patterns = group->regExp().pattern().split(',');
661 
662  //find all urls where a group can be identified
663  KUrl::List::iterator it = urls.begin();
664  while (it != urls.end()) {
665  const KUrl sourceUrl = *it;
666  if (KGet::matchesExceptions(sourceUrl, patterns)) {
667  const KUrl destUrl = UrlChecker::destUrl(folderUrl, sourceUrl);
668  newDest = check.checkExistingFile(sourceUrl, destUrl);
669  if (!newDest.isEmpty()) {
670  data << KGet::TransferData(sourceUrl, newDest, groupName);
671  }
672  it = urls.erase(it);
673  } else {
674  ++it;
675  }
676  }
677  }
678 
679  //there are still some unhandled urls, i.e. for those no group could be found, add them with an empty group
680  foreach (const KUrl &sourceUrl, urls) {
681  const KUrl destUrl = UrlChecker::destUrl(folderUrl, sourceUrl);
682  newDest = check.checkExistingFile(sourceUrl, destUrl);
683  if (!newDest.isEmpty()) {
684  data << KGet::TransferData(sourceUrl, newDest);
685  }
686  }
687 
688  //all urls have been handled
689  urls.clear();
690  }
691 
693  kDebug(5001) << "DIRECTORIES AS SUGGESTION" << Settings::directoriesAsSuggestion();
694  if (!Settings::directoriesAsSuggestion() && !urls.isEmpty()) {
695  kDebug(5001) << "No, Directories not as suggestion";
696 
697  //find the associated groups first, we just need the first matching group though
698  const QList<TransferGroupHandler*> groups = KGet::allTransferGroups();
699  foreach (TransferGroupHandler *group, groups) {
700  if (urls.isEmpty()) {
701  break;
702  }
703 
704  const KUrl folderUrl = KUrl(group->defaultFolder());
705  if (UrlChecker::checkFolder(folderUrl) != UrlChecker::NoError) {
706  continue;
707  }
708 
709  const QString groupName = group->name();
710  const QStringList patterns = group->regExp().pattern().split(',');
711 
712  KUrl::List::iterator it = urls.begin();
713  while (it != urls.end()) {
714  const KUrl sourceUrl = *it;
715  if (KGet::matchesExceptions(sourceUrl, patterns)) {
716  const KUrl destUrl = UrlChecker::destUrl(folderUrl, sourceUrl);
717  newDest = check.checkExistingFile(sourceUrl, destUrl);
718  if (!newDest.isEmpty()) {
719  data << KGet::TransferData(sourceUrl, newDest, groupName);
720  }
721 
722  it = urls.erase(it);
723  } else {
724  ++it;
725  }
726  }
727  }
728  }
729 
731  if (!Settings::askForDestination()) {
732  //the download path will be always used
733  const QString dir = KGlobalSettings::downloadPath();
734  if (!dir.isEmpty()) {
735  KUrl::List::iterator it = urls.begin();
736  while (it != urls.end()) {
737  const KUrl sourceUrl = *it;
738  const KUrl destUrl = UrlChecker::destUrl(dir, sourceUrl);
739  newDest = check.checkExistingFile(sourceUrl, destUrl);
740  if (!newDest.isEmpty()) {
741  data << KGet::TransferData(sourceUrl, newDest);
742  }
743 
744  it = urls.erase(it);
745  }
746  }
747  }
748 
750  if (!data.isEmpty()) {
751  KGet::createTransfers(data);
752  }
753 
755  if ((!m_dialog || m_dialog->isEmpty()) && urls.count() == 1) {//FIXME why the m_dialog check? whenever a dialog has been created this would not be shown?
756  KUrl url = urls.first();
757  QPointer<KDialog> dialog;
758  foreach (TransferFactory * factory, KGet::factories()) {
759  const QList<TransferGroupHandler*> groups = KGet::groupsFromExceptions(url);
760  dialog = factory->createNewTransferDialog(url, suggestedFileName, !groups.isEmpty() ? groups.first() : 0);
761  if (dialog) {
762  KWindowInfo info = KWindowSystem::windowInfo(KGet::m_mainWindow->winId(), NET::WMDesktop, NET::WMDesktop);
763  KWindowSystem::setCurrentDesktop(info.desktop());
764  KWindowSystem::forceActiveWindow(KGet::m_mainWindow->winId());
765 
766  dialog->exec();
767  delete dialog;
768  }
769  }
770  }
771 
772  m_numJobs.remove(jobId);
773  m_urls.erase(itUrls);
774 
776  if (!urls.isEmpty()) {
777  createDialog(urls, suggestedFileName);
778  }
779 }
780 
781 void NewTransferDialogHandler::createDialog(const KUrl::List &urls, const QString &suggestedFileName)
782 {
783  if (!m_dialog) {
784  m_dialog = new NewTransferDialog(KGet::m_mainWindow);
785  }
786 
787  m_dialog->m_window = KGet::m_mainWindow;
788  m_dialog->showDialog(urls, suggestedFileName);
789 }
790 
791 #include "newtransferdialog.moc"
UrlChecker
This class provides static methods to check if urls are valid, and if not provides enums to see what ...
Definition: urlchecker.h:34
KGet::generalDestDir
static QString generalDestDir(bool preferXDGDownloadDir=false)
Returns a download directory.
Definition: kget.cpp:945
NewTransferDialog::setDefaultDestination
void setDefaultDestination()
Called when the transfer group or the urlREquester changed, the dialog sets the default destination f...
Definition: newtransferdialog.cpp:234
KGet::transferGroupNames
static QStringList transferGroupNames()
Definition: kget.cpp:168
UrlChecker::NoError
Definition: urlchecker.h:61
TransferFactory::createNewTransferDialog
virtual KDialog * createNewTransferDialog(const KUrl &srcUrl, const QString &suggestedFileName=QString(), TransferGroupHandler *defaultGroup=0)
Definition: transferfactory.cpp:50
TransferGroupHandler
Definition: transfergrouphandler.h:30
transfertreemodel.h
QWidget
NewTransferDialogHandler
Definition: newtransferdialog.h:104
Settings::directoriesAsSuggestion
static bool directoriesAsSuggestion()
Get DirectoriesAsSuggestion.
Definition: settings.h:810
mostlocalurl.h
urlchecker.h
UrlChecker::checkDestination
static UrlError checkDestination(const KUrl &destination, bool showNotification=false)
Convenience method of checkUrl.
Definition: urlchecker.cpp:166
KDialog
TransferGroupHandler::iconName
QString iconName() const
Definition: transfergrouphandler.h:193
QObject
mostLocalUrl
KUrl mostLocalUrl(const KUrl &url)
Synchronous.
Definition: mostlocalurl.cpp:27
KGet::matchesExceptions
static bool matchesExceptions(const KUrl &sourceUrl, const QStringList &patterns)
Returns true if sourceUrl matches any of the patterns.
Definition: kget.cpp:743
NewTransferDialog::~NewTransferDialog
~NewTransferDialog()
Definition: newtransferdialog.cpp:87
mostLocalUrlJob
MostLocalUrlJob * mostLocalUrlJob(const KUrl &url)
Asynchronous.
Definition: mostlocalurl.cpp:41
KGet::allTransferGroups
static QList< TransferGroupHandler * > allTransferGroups()
Gets all transfer-groups.
Definition: kget.cpp:668
FileDeleter::deleteFile
static KJob * deleteFile(const KUrl &dest, QObject *receiver=0, const char *method=0)
Starts the deletion of dest and emits KJob::finished once done.
Definition: filedeleter.cpp:73
Settings::self
static Settings * self()
Definition: settings.cpp:17
transferfactory.h
UrlChecker::Folder
Definition: urlchecker.h:40
UrlChecker::UrlError
UrlError
Definition: urlchecker.h:59
TransferGroupHandler::name
const QString & name()
Definition: transfergrouphandler.h:83
UrlChecker::Source
Definition: urlchecker.h:38
transfergrouphandler.h
UrlChecker::destUrl
static KUrl destUrl(const KUrl &destOrFolder, const KUrl &source, const QString &fileName=QString())
Takes an url to a source and an url to either the destination or a folder and returns a destination u...
Definition: urlchecker.cpp:225
MostLocalUrlJob::mostLocalUrl
KUrl mostLocalUrl() const
Call this in the slot connected to result.
Definition: mostlocalurl.cpp:57
mainwindow.h
TransferGroupHandler::defaultFolder
QString defaultFolder()
Definition: transfergrouphandler.h:134
newtransferdialog.h
NewTransferDialog
Dialog to allow add one or more transfers to kget.
Definition: newtransferdialog.h:35
MostLocalUrlJob
Job for asynchronously getting the most local url, do not use directly, but use mostLocalUrlJob inste...
Definition: mostlocalurl.h:54
NewTransferDialogHandler::NewTransferDialogHandler
NewTransferDialogHandler(QObject *parent=0)
NOTE some checks in this class might seem redundant, though target is to display as few dialogs...
Definition: newtransferdialog.cpp:482
UrlChecker::message
static QString message(const KUrl &url, const UrlType type, const UrlError error)
Get a describing message for UrlError.
Definition: urlchecker.cpp:290
UrlChecker::existingTransfer
static TransferHandler * existingTransfer(const KUrl &url, const UrlType type, UrlWarning *warning=0)
Checks if there is an existing transfer for url with type.
Definition: urlchecker.cpp:242
MostLocalUrlJob::url
KUrl url()
Definition: mostlocalurl.cpp:52
Settings::askForDestination
static bool askForDestination()
Get AskForDestination.
Definition: settings.h:829
KGet::groupsFromExceptions
static QList< TransferGroupHandler * > groupsFromExceptions(const KUrl &filename)
Definition: kget.cpp:730
UrlChecker::NoWarning
Definition: urlchecker.h:74
UrlChecker::checkSource
static UrlError checkSource(const KUrl &source, bool showNotification=false)
Convenience method of checkUrl.
Definition: urlchecker.cpp:139
Settings::lastGroup
static QString lastGroup()
Get LastGroup.
Definition: settings.h:962
filedeleter.h
settings.h
UrlChecker::removeDuplicates
static void removeDuplicates(KUrl::List &urls)
Removes duplicates of a list of urls.
Definition: urlchecker.cpp:113
NewTransferDialogHandler::showNewTransferDialog
static void showNewTransferDialog(const KUrl &url=KUrl())
Definition: newtransferdialog.cpp:493
TransferGroupHandler::regExp
QRegExp regExp()
Definition: transfergrouphandler.h:145
UrlChecker::Destination
Definition: urlchecker.h:39
Settings::setLastGroup
static void setLastGroup(const QString &v)
Set LastGroup.
Definition: settings.h:952
NewTransferDialogHandler::~NewTransferDialogHandler
~NewTransferDialogHandler()
Definition: newtransferdialog.cpp:488
kget.h
KGet::TransferData
TransferData(const KUrl &src, const KUrl &dest, const QString &groupName=QString(), bool start=false, const QDomElement *e=0)
UrlChecker::UrlWarning
UrlWarning
Definition: urlchecker.h:73
TransferFactory
TransferFactory.
Definition: transferfactory.h:52
UrlChecker::ExistingFile
Definition: urlchecker.h:80
UrlChecker::checkFolder
static UrlError checkFolder(const KUrl &folder, bool showNotification=false)
Convenience method of checkUrl.
Definition: urlchecker.cpp:195
KGet::factories
static QList< TransferFactory * > factories()
Definition: kget.cpp:621
UrlChecker::wouldOverwrite
static bool wouldOverwrite(const KUrl &source, const KUrl &dest)
Checks if source is local and exists already.
Definition: urlchecker.cpp:134
KJob
Settings::setLastDirectory
static void setLastDirectory(const QString &v)
Set LastDirectory.
Definition: settings.h:933
KGet::delTransfer
static bool delTransfer(TransferHandler *transfer, DeleteMode mode=AutoDelete)
Removes a transfer from the KGet.
Definition: kget.cpp:362
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal