Relicense all GPLv2 only code to GPLv2+.
[kdenetwork.git] / kget / ui / newtransferdialog.cpp
blob665c91371ae19ea1481735b6ec88682c74874ddd
1 /* This file is part of the KDE project
3 Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4 Copyright (C) 2007 by Javier Goday <jgoday@gmail.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
12 #include "newtransferdialog.h"
14 #include "core/kget.h"
15 #include "core/transfertreemodel.h"
16 #include "settings.h"
18 #include <QWidget>
19 #include <QApplication>
20 #include <QClipboard>
22 #include <KLocale>
23 #include <KListWidget>
24 #include <QListWidgetItem>
25 #include <KLineEdit>
26 #include <KComboBox>
27 #include <KDebug>
29 NewTransferDialog::NewTransferDialog(QWidget *parent)
30 : KDialog(parent),
31 m_multiple(false)
33 setCaption(i18n("New Download"));
34 setMinimumSize(400, 300);
35 showButtonSeparator(true);
37 QWidget *mainWidget = new QWidget(this);
39 Ui::NewTransferWidget widget;
40 widget.setupUi(mainWidget);
42 m_gridLayout = widget.gridLayout;
43 m_titleWidget = widget.titleWidget;
44 m_folderRequester = widget.folderRequester;
45 m_groupComboBox = widget.groupComboBox;
46 m_defaultFolderButton = widget.defaultFolderButton;
47 setMainWidget(mainWidget);
48 prepareGui();
51 void NewTransferDialog::showNewTransferDialog(const QString &srcUrl)
53 NewTransferDialog *dialog = new NewTransferDialog();
54 dialog->setMultiple(false);
55 dialog->setSource(srcUrl);
57 NewTransferDialog::showNewTransferDialog(dialog);
60 void NewTransferDialog::showNewTransferDialog(const KUrl::List &list)
62 NewTransferDialog *dialog = new NewTransferDialog();
63 dialog->setMultiple(true);
64 dialog->setSource(list);
66 NewTransferDialog::showNewTransferDialog(dialog);
69 bool NewTransferDialog::multiple() const
71 return m_multiple;
74 void NewTransferDialog::setMultiple(bool value)
76 m_multiple = value;
77 if (multiple())
79 listWidget = new KListWidget();
80 m_gridLayout->addWidget(listWidget, 0, 1, 1, 1);
82 else
84 urlRequester = new KLineEdit();
85 urlRequester->setClearButtonShown(true);
86 m_gridLayout->addWidget(urlRequester, 0, 1, 1, 1);
90 void NewTransferDialog::setSource(const QString &srcUrl)
92 urlRequester->clear();
93 if (srcUrl.isEmpty())
95 KUrl clipboardUrl = KUrl(QApplication::clipboard()->text(QClipboard::Clipboard).trimmed());
96 if (clipboardUrl.isValid())
97 urlRequester->insert(clipboardUrl.url());
99 else
101 urlRequester->insert(srcUrl);
105 void NewTransferDialog::setSource(const KUrl::List &list)
107 KUrl::List::const_iterator it = list.begin();
108 KUrl::List::const_iterator itEnd = list.end();
110 for (; it!=itEnd ; ++it)
112 QListWidgetItem *newItem = new QListWidgetItem(it->url(), listWidget);
113 newItem->setCheckState(Qt::Checked);
117 KUrl::List NewTransferDialog::source() const
119 KUrl::List list;
120 if (multiple())
122 foreach (QListWidgetItem *item, listWidget->findItems(QString('*'), Qt::MatchWrap | Qt::MatchWildcard))
124 if (item->checkState() == Qt::Checked)
125 list.append(KUrl(item->text()));
128 else
130 list.append(KUrl(urlRequester->text()));
132 return list;
135 void NewTransferDialog::setDestination(const QString &destination)
137 m_folderRequester->comboBox()->insertItem(0, destination);
138 m_folderRequester->comboBox()->setCurrentIndex(0);
141 QString NewTransferDialog::destination() const
143 return m_folderRequester->url().url();
146 QString NewTransferDialog::transferGroup() const
148 return m_groupComboBox->currentText();
151 void NewTransferDialog::showNewTransferDialog(NewTransferDialog *dialog)
153 QString destDir;
155 if (Settings::useDefaultDirectory())
156 #ifdef Q_OS_WIN //krazy:exclude=cpp
157 destDir = Settings::defaultDirectory().remove("file:///");
158 #else
159 destDir = Settings::defaultDirectory().remove("file://");
160 #endif
162 if (!dialog->source().isEmpty())
164 QString checkExceptions = KGet::getSaveDirectoryFromExceptions(dialog->source().takeFirst());
166 if (Settings::enableExceptions() && !checkExceptions.isEmpty())
167 destDir = checkExceptions;
170 if (!Settings::lastDirectory().isEmpty() &&
171 QString::compare(Settings::lastDirectory(), Settings::defaultDirectory()) != 0)
173 dialog->setDestination(Settings::lastDirectory());
176 if (!destDir.isEmpty())
177 dialog->setDestination(destDir);
179 dialog->exec();
181 KUrl::List srcUrls = dialog->source();
182 destDir = dialog->destination();
184 if (dialog->result() == KDialog::Accepted)
186 #ifdef Q_OS_WIN //krazy:exclude=cpp
187 destDir = destDir.remove("file:///");
188 #else
189 destDir = destDir.remove("file://");
190 #endif
191 if(dialog->m_defaultFolderButton->checkState() == Qt::Checked)
193 Settings::setDefaultDirectory(destDir);
194 Settings::setUseDefaultDirectory(true);
195 Settings::self()->writeConfig();
197 else
199 Settings::setLastDirectory(destDir);
200 Settings::self()->writeConfig();
202 kDebug(5001) << srcUrls;
203 if (srcUrls.count() == 1)
204 KGet::addTransfer(srcUrls.takeFirst(), destDir, dialog->transferGroup());
205 else
206 KGet::addTransfer(srcUrls, destDir, dialog->transferGroup());
210 void NewTransferDialog::prepareGui()
212 // Only select directories
213 m_folderRequester->setMode(KFile::Directory);
215 // properties of the m_folderRequester combobox
216 m_folderRequester->comboBox()->setDuplicatesEnabled(false);
218 // transfer groups
219 m_groupComboBox->addItems(KGet::transferGroupNames());
221 m_groupComboBox->setCurrentIndex(0);
222 m_titleWidget->setPixmap(KIcon("document-new").pixmap(22, 22), KTitleWidget::ImageLeft);
225 #include "newtransferdialog.moc"