use KDE standard for toolbar icon's text
[kdenetwork.git] / kget / ui / transferhistory.cpp
blobec3c5f1252b36717e3b89e09787a2c5acc124758
1 /* This file is part of the KDE project
3 Copyright (C) 2007 by Lukas Appelhans <l.appelhans@gmx.de>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9 */
11 #include "transferhistory.h"
12 #include "ui/newtransferdialog.h"
13 #include "settings.h"
15 #include <QFile>
16 #include <QDomElement>
17 #include <QDomNodeList>
18 #include <QMenu>
19 #include <QFileSystemWatcher>
20 #include <QFontMetrics>
21 #include <QDateTime>
22 #include <QStandardItem>
24 #include <KDebug>
25 #include <KStandardDirs>
26 #include <KMessageBox>
27 #include <KToolBar>
28 #include <KTreeWidgetSearchLine>
29 #include <KRun>
30 #include <KGlobalSettings>
31 #include <kio/global.h>
33 TransferHistory::TransferHistory(QWidget *parent)
34 : KDialog(parent),
35 m_rangeType(0)
37 setCaption(i18n("Transfer History"));
38 setButtons(KDialog::Close);
39 //Setup Ui-Parts from Designer
40 QWidget *mainWidget = new QWidget(this);
42 Ui::TransferHistory widget;
43 widget.setupUi(mainWidget);
45 m_treeWidget = new RangeTreeWidget(this);
46 slotLoadRangeType(TransferHistory::Date);
48 // range type
49 m_rangeTypeCombobox = widget.rangeType;
51 m_verticalLayout = widget.vboxLayout;
52 m_hboxLayout = widget.hboxLayout;
53 m_searchBar = widget.searchBar;
54 //m_searchBar->setTreeWidget(m_treeWidget);
55 m_clearButton = widget.clearButton;
56 m_clearButton->setIcon(KIcon("edit-clear-history"));
57 m_actionDelete_Selected = widget.actionDelete_Selected;
58 m_actionDelete_Selected->setIcon(KIcon("edit-delete"));
59 m_actionDownload = widget.actionDownload;
60 m_actionDownload->setIcon(KIcon("document-new"));
61 m_openFile = new QAction(KIcon("document-open"), "&Open File", this);
62 setMainWidget(mainWidget);
63 setInitialSize(QSize(800, 400));
65 m_verticalLayout->addWidget(m_treeWidget);
67 watcher = new QFileSystemWatcher();
68 watcher->addPath(KStandardDirs::locateLocal("appdata", QString()));
69 kDebug(5001) << watcher->directories();
71 connect(m_actionDelete_Selected, SIGNAL(triggered()), this, SLOT(slotDeleteTransfer()));
72 connect(m_actionDownload, SIGNAL(triggered()), this, SLOT(slotDownload()));
73 connect(m_openFile, SIGNAL(triggered()), this, SLOT(slotOpenFile()));
74 connect(m_clearButton, SIGNAL(clicked()), this, SLOT(slotClear()));
75 connect(watcher, SIGNAL(directoryChanged(const QString &)), this, SLOT(slotLoadRangeType()));
76 connect(m_rangeTypeCombobox, SIGNAL(activated(int)), this, SLOT(slotLoadRangeType(int)));
79 void TransferHistory::slotDeleteTransfer()
81 QString transferName = m_treeWidget->currentItem(0)->text();
82 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
83 QString error;
84 int line;
85 int column;
87 m_treeWidget->removeRow(m_treeWidget->currentIndex().row(), m_treeWidget->currentIndex().parent());
89 QDomDocument doc("tempHistory");
90 QFile file(filename);
91 if (!doc.setContent(&file, &error, &line, &column))
93 kDebug(5001) << "Error1" << error << line << column;
94 file.close();
95 return;
97 file.close();
99 QDomElement root = doc.documentElement();
101 kDebug(5001) << "Load file" << filename;
103 QDomNodeList list = root.elementsByTagName("Transfer");
105 int nItems = list.length();
107 QDomElement elementToRemove;
108 for (int i = 0 ; i < nItems ; i++)
110 QDomElement element = list.item(i).toElement();
111 kDebug(5001) << element.attribute("Source", "") << "----" << transferName;
112 if (element.attribute("Source", "") == transferName) {
113 elementToRemove = element;
117 doc.documentElement().removeChild(elementToRemove);
118 file.remove();
120 if (!file.open(QIODevice::ReadWrite)) {
121 KMessageBox::error(0, i18n("History-File cannot be opened correctly!"), i18n("Error"), 0);
123 else {
124 QTextStream stream(&file);
125 doc.save(stream, 0);
126 file.close();
129 // reload the treewidget
130 slotLoadRangeType(m_rangeType);
133 void TransferHistory::slotAddTransfers()
135 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
136 QString error;
137 int line;
138 int column;
140 QDomDocument doc("tempHistory");
141 QFile file(filename);
142 if (!doc.setContent(&file, &error, &line, &column))
144 kDebug(5001) << "Error1" << error << line << column;
145 file.close();
146 return;
149 QDomElement root = doc.documentElement();
151 kDebug(5001) << "Load file" << filename;
153 QDomNodeList list = root.elementsByTagName("Transfer");
155 int nItems = list.length();
157 for (int i = 0 ; i < nItems ; i++)
159 QDomElement dom = list.item(i).toElement();
160 defaultItems.append(dom);
161 QVariantList attributeList;
162 attributeList.append(dom.attribute("Source"));
163 attributeList.append(dom.attribute("Dest"));
164 attributeList.append(QDateTime::fromString(dom.attribute("Time")).date().toString());
165 attributeList.append(KIO::convertSize(dom.attribute("Size").toInt()));
166 attributeList.append(dom.attribute("State"));
168 int data = 0;
169 if(m_rangeType == TransferHistory::Date) {
170 QDate date = QDateTime::fromString(dom.attribute("Time")).date();
171 data = date.daysTo(QDate::currentDate());
173 else {
174 data = dom.attribute("Size").toInt();
175 kDebug() << "Setting data to " << data;
177 m_treeWidget->add(data, attributeList);
179 doc.clear();
180 file.close();
183 void TransferHistory::slotClear()
185 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
186 QFile file(filename);
187 file.remove();
188 slotLoadRangeType();
191 void TransferHistory::slotWriteDefault()
193 kDebug(5001);
194 if (!save)
196 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
197 QFile file(filename);
198 file.remove();
199 QDomDocument *doc;
200 QDomElement root;
202 if (!file.exists())
204 doc = new QDomDocument("Transfers");
205 root = doc->createElement("Transfers");
206 doc->appendChild(root);
208 else
210 doc = new QDomDocument();
211 doc->setContent(&file);
212 file.close();
213 root = doc->documentElement();
214 doc->appendChild(root);
217 foreach (QDomElement e, defaultItems)
219 kDebug(5001) << e.attribute("Source");
220 root.appendChild(e);
223 if (!file.open(QIODevice::ReadWrite))
224 KMessageBox::error(0, i18n("History-File cannot be opened correctly!"), i18n("Error"), 0);
226 QTextStream stream(&file);
227 doc->save(stream, 0);
228 file.close();
232 void TransferHistory::slotDownload()
234 NewTransferDialog::showNewTransferDialog(m_treeWidget->currentItem(0)->text());
237 void TransferHistory::contextMenuEvent(QContextMenuEvent *event)
239 Q_UNUSED(event)
241 if(m_treeWidget->currentIndex().parent().isValid()) {
242 QMenu *contextMenu = new QMenu(this);
243 contextMenu->addAction(m_actionDownload);
244 contextMenu->addAction(m_actionDelete_Selected);
246 if (m_treeWidget->currentItem(4)->text() == "Finished")
247 contextMenu->addAction(m_openFile);
248 contextMenu->exec(QCursor::pos());
252 void TransferHistory::slotOpenFile()
254 new KRun(m_treeWidget->currentItem(1)->text(), this, true, false);
257 void TransferHistory::hideEvent(QHideEvent *event)
259 Q_UNUSED(event);
260 disconnect(watcher, SIGNAL(directoryChanged(const QString &)), this, SLOT(slotAddTransfers()));//Prevent reloading of TransferHistory when saving
261 deleteLater();
264 void TransferHistory::slotLoadRangeType(int type)
266 if(type >= 0) {
267 m_rangeType = type;
270 QFontMetrics *font = new QFontMetrics(KGlobalSettings::generalFont());
271 m_treeWidget->clear();
273 m_treeWidget->setLabels(QStringList() << "Source-File" << "Destination" << "Time" << "File Size" << "Status");
275 switch(m_rangeType)
277 case TransferHistory::Size :
278 m_treeWidget->addRange(0, 1024 * 1024, i18n("Less than 1MiB"));
279 m_treeWidget->addRange(1024 * 1024, 1024 * 1024 * 10, i18n("Between 1MiB-10MiB"));
280 m_treeWidget->addRange(1024 * 1024 * 10, 1024 * 1024 * 100, i18n("Between 10MiB-100MiB"));
281 m_treeWidget->addRange(1024 * 1024 * 100, 1024 * 1024 *1024, i18n("Between 100MiB-1GiB"));
282 m_treeWidget->addRange(1024 * 1024 * 1024, 1024 * 1024 * 1024 * 10, i18n("More than 1GiB"));
283 break;
284 default:
285 m_treeWidget->addRange(0, 1, i18n("Today"));
286 m_treeWidget->addRange(1, 7, i18n("Last week"));
287 m_treeWidget->addRange(7, 30, i18n("Last month"));
288 m_treeWidget->addRange(30, -1, i18n("A long time ago"));
291 QList<int> list = Settings::historyColumnWidths();
293 if (!list.isEmpty())
295 int j = 0;
296 foreach (int i, list)
298 m_treeWidget->setColumnWidth(j, i);
299 j++;
302 else
304 kDebug(5001);
305 m_treeWidget->setColumnWidth(0, 200);
306 m_treeWidget->setColumnWidth(1, 250);
307 m_treeWidget->setColumnWidth(2, font->width(QDate::currentDate().toString()));
308 m_treeWidget->setColumnWidth(3, font->width("1500000 KiB"));
309 m_treeWidget->setColumnWidth(4, font->width(i18nc("the transfer has been finished", "Finished")));
312 slotAddTransfers();