Fix missing files =)
[kdenetwork.git] / kget / ui / transferhistory.cpp
blob7c698df41b006f987d9488745773ae7d3f477cd8
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"
14 #include <QFile>
15 #include <QDomElement>
16 #include <QDomNodeList>
17 #include <QMenu>
18 #include <QFileSystemWatcher>
20 #include <KDebug>
21 #include <KStandardDirs>
22 #include <KMessageBox>
23 #include <KToolBar>
24 #include <KTreeWidgetSearchLine>
25 #include <KRun>
27 TransferHistory::TransferHistory(QWidget *parent)
28 : KDialog(parent)
30 //Setup Ui-Parts from Designer
31 QWidget *mainWidget = new QWidget(this);
33 Ui::TransferHistory widget;
34 widget.setupUi(mainWidget);
36 m_gridLayout = widget.gridLayout;
37 m_treeWidget = widget.treeWidget;
38 m_treeWidget->setRootIsDecorated(false);
39 m_treeWidget->setColumnWidth(0, 310);
40 m_treeWidget->setColumnWidth(1, 214);
41 m_treeWidget->setColumnWidth(2, 180);
42 m_treeWidget->setColumnWidth(3, 64);
43 m_hboxLayout = widget.hboxLayout;
44 m_searchLabel = widget.searchLabel;
45 m_searchBar = widget.searchBar;
46 m_searchBar->setTreeWidget(m_treeWidget);
47 m_clearButton = widget.clearButton;
48 m_clearButton->setIcon(KIcon("edit-clear-history"));
49 m_actionDelete_Selected = widget.actionDelete_Selected;
50 m_actionDelete_Selected->setIcon(KIcon("edit-delete"));
51 m_actionDownload = widget.actionDownload;
52 m_actionDownload->setIcon(KIcon("document-new"));
53 m_openFile = new QAction(KIcon("document-open"), "&Open File", this);
54 setMainWidget(mainWidget);
55 setInitialSize(QSize(800, 400));
57 slotAddTransfers();
59 watcher = new QFileSystemWatcher();
60 watcher->addPath(KStandardDirs::locateLocal("appdata", QString()));
61 kDebug(5001) << watcher->directories();
63 /**connect(actionReloadHistory, SIGNAL(triggered()), this, SLOT(slotAddTransfers()));**/
64 connect(m_actionDelete_Selected, SIGNAL(triggered()), this, SLOT(slotDeleteTransfer()));
65 connect(m_actionDownload, SIGNAL(triggered()), this, SLOT(slotDownload()));
66 connect(m_openFile, SIGNAL(triggered()), this, SLOT(slotOpenFile()));
67 connect(m_clearButton, SIGNAL(clicked()), this, SLOT(slotClear()));
68 connect(watcher, SIGNAL(directoryChanged()), this, SLOT(addTransfers()));
69 connect(this, SIGNAL(accepted()), this, SLOT(slotSave()));
70 connect(this, SIGNAL(rejected()), this, SLOT(slotWriteDefault()));
73 void TransferHistory::slotDeleteTransfer()
75 int index = m_treeWidget->indexOfTopLevelItem(m_treeWidget->currentItem());
76 delete m_treeWidget->takeTopLevelItem(index);
79 void TransferHistory::slotAddTransfers()
81 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
82 bool errorbool;
83 QString error;
84 int line;
85 int column;
87 QDomDocument doc("tempHistory");
88 QFile file(filename);
89 if (!file.open(QIODevice::ReadOnly))
90 return;
91 if (!doc.setContent(&file, &errorbool, &error, &line, &column))
93 kDebug(5001) << "Error1" << error << line << column;
94 file.close();
95 return;
97 if(file.reset())
98 kDebug(5001) << "resettingn the file, to have a clean starting point to readd";
99 file.close();
101 QDomElement root = doc.documentElement();
103 kDebug(5001) << "Load file" << filename;
105 QDomNodeList list = root.elementsByTagName("Transfer");
107 int nItems = list.length();
109 for (int i = 0 ; i < nItems ; i++)
111 QDomElement dom = list.item(i).toElement();
112 defaultItems.append(dom);
113 QStringList attributeList;
114 attributeList.append(dom.attribute("Source"));
115 attributeList.append(dom.attribute("Dest"));
116 attributeList.append(dom.attribute("Time"));
117 attributeList.append(dom.attribute("State"));
118 QTreeWidgetItem *item = new QTreeWidgetItem(attributeList);
119 m_treeWidget->addTopLevelItem(item);
120 kDebug(5001) << attributeList;
122 foreach (QDomElement element, defaultItems)
124 kDebug(5001) << element.attribute("Source");
127 doc.clear();
128 file.remove();
131 void TransferHistory::slotClear()
133 m_treeWidget->clear();
136 void TransferHistory::slotWriteDefault()
138 if (!save)
140 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
141 QFile file(filename);
142 file.remove();
143 QDomDocument *doc;
144 QDomElement root;
146 if (!file.exists())
148 doc = new QDomDocument("Transfers");
149 root = doc->createElement("Transfers");
150 doc->appendChild(root);
152 else
154 doc = new QDomDocument();
155 doc->setContent(&file);
156 file.close();
157 root = doc->documentElement();
158 doc->appendChild(root);
161 foreach (QDomElement e, defaultItems)
163 kDebug(5001) << e.attribute("Source");
164 root.appendChild(e);
167 if (!file.open(QIODevice::ReadWrite))
168 KMessageBox::error(0, i18n("History-File cannot be opened correctly!"), i18n("Error"), 0);
170 QTextStream stream(&file);
171 doc->save(stream, 0);
172 file.close();
176 void TransferHistory::slotSave()
178 QString filename = KStandardDirs::locateLocal("appdata", "transferhistory.kgt");
179 QFile file(filename);
180 file.remove();
181 QDomDocument *doc;
182 QDomElement root;
184 if (!file.exists())
186 doc = new QDomDocument("Transfers");
187 root = doc->createElement("Transfers");
188 doc->appendChild(root);
190 else
192 doc = new QDomDocument();
193 doc->setContent(&file);
194 file.close();
195 root = doc->documentElement();
196 doc->appendChild(root);
199 foreach (QTreeWidgetItem *item, m_treeWidget->findItems("", Qt::MatchContains | Qt::MatchRecursive))
201 QDomElement e = doc->createElement("Transfer");
202 root.appendChild(e);
204 e.setAttribute("Source", item->text(0));
205 e.setAttribute("Dest", item->text(1));
206 e.setAttribute("Time", item->text(2));
207 e.setAttribute("State", item->text(3));
209 kDebug(5001) << e.attribute("Source");
212 if (!file.open(QIODevice::ReadWrite))
213 KMessageBox::error(0, i18n("History-File cannot be opened correctly!"), i18n("Error"), 0);
215 QTextStream stream(&file);
216 doc->save(stream, 0);
217 file.close();
218 save = true;
220 close();
223 void TransferHistory::slotDownload()
225 if (m_treeWidget->indexOfTopLevelItem(m_treeWidget->currentItem()) == -1)
226 return;
227 NewTransferDialog::showNewTransferDialog(m_treeWidget->currentItem()->text(0));
228 slotSave();
231 void TransferHistory::contextMenuEvent(QContextMenuEvent *event)
233 if (m_treeWidget->indexOfTopLevelItem(m_treeWidget->currentItem()) == -1)
234 return;
235 QMenu *contextMenu = new QMenu(this);
236 contextMenu->addAction(m_actionDownload);
237 contextMenu->addAction(m_actionDelete_Selected);
238 if (m_treeWidget->currentItem()->text(3) == "Finished")
239 contextMenu->addAction(m_openFile);
240 contextMenu->exec(QCursor::pos());
243 void TransferHistory::slotOpenFile()
245 new KRun(m_treeWidget->currentItem()->text(1), this, true, false);