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"
19 #include <QApplication>
23 #include <KListWidget>
24 #include <QListWidgetItem>
29 NewTransferDialog::NewTransferDialog(QWidget
*parent
)
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
);
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
74 void NewTransferDialog::setMultiple(bool value
)
79 listWidget
= new KListWidget();
80 m_gridLayout
->addWidget(listWidget
, 0, 1, 1, 1);
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();
95 KUrl clipboardUrl
= KUrl(QApplication::clipboard()->text(QClipboard::Clipboard
).trimmed());
96 if (clipboardUrl
.isValid())
97 urlRequester
->insert(clipboardUrl
.url());
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
122 foreach (QListWidgetItem
*item
, listWidget
->findItems(QString('*'), Qt::MatchWrap
| Qt::MatchWildcard
))
124 if (item
->checkState() == Qt::Checked
)
125 list
.append(KUrl(item
->text()));
130 list
.append(KUrl(urlRequester
->text()));
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
)
155 if (Settings::useDefaultDirectory())
156 #ifdef Q_OS_WIN //krazy:exclude=cpp
157 destDir
= Settings::defaultDirectory().remove("file:///");
159 destDir
= Settings::defaultDirectory().remove("file://");
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
);
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:///");
189 destDir
= destDir
.remove("file://");
191 if(dialog
->m_defaultFolderButton
->checkState() == Qt::Checked
)
193 Settings::setDefaultDirectory(destDir
);
194 Settings::setUseDefaultDirectory(true);
195 Settings::self()->writeConfig();
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());
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);
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"