1 /* This file is part of the KDE project
3 Copyright (C) 2007 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.
10 #include "btfiletreeview.h"
12 #include <util/bitset.h>
13 #include <util/functions.h>
14 #include <interfaces/torrentinterface.h>
15 #include <interfaces/torrentfileinterface.h>
21 #include <kmessagebox.h>
26 BTFileTreeView::BTFileTreeView(bt::TorrentInterface
*tc
, QWidget
* parent
)
30 fileTreeModel
= new kt::IWFileTreeModel(tc
, this);
31 setModel(fileTreeModel
);
33 if (!m_tc
->getStats().multi_file_torrent
)
34 setRootIsDecorated(false);
36 contextMenu
= new KMenu(this);
37 open_action
= contextMenu
->addAction(KIcon("document-open"),i18n("Open"),this,SLOT(open()));
38 contextMenu
->addSeparator();
39 download_first_action
= contextMenu
->addAction(i18n("Download first"),this,SLOT(downloadFirst()));
40 download_normal_action
= contextMenu
->addAction(i18n("Download normally"),this,SLOT(downloadNormal()));
41 download_last_action
= contextMenu
->addAction(i18n("Download last"),this,SLOT(downloadLast()));
42 contextMenu
->addSeparator();
43 dnd_action
= contextMenu
->addAction(i18n("Do Not Download"),this,SLOT(doNotDownload()));
44 delete_action
= contextMenu
->addAction(i18n("Delete File(s)"),this,SLOT(deleteFiles()));
46 //connect(this, SIGNAL(customContextMenuRequested(const QPoint & )), this, SLOT(showContextMenu(const QPoint& )));
49 void BTFileTreeView::contextMenuEvent(QContextMenuEvent
* e
)
52 const TorrentStats
& s
= m_tc
->getStats();
54 QModelIndexList sel
= selectionModel()->selectedRows();
60 download_first_action
->setEnabled(true);
61 download_normal_action
->setEnabled(true);
62 download_last_action
->setEnabled(true);
63 open_action
->setEnabled(false);
64 dnd_action
->setEnabled(true);
65 delete_action
->setEnabled(true);
66 contextMenu
->popup(QCursor::pos());
70 QModelIndex item
= sel
.front();
71 bt::TorrentFileInterface
* file
= fileTreeModel
->indexToFile(item
);
73 download_first_action
->setEnabled(false);
74 download_last_action
->setEnabled(false);
75 download_normal_action
->setEnabled(false);
76 dnd_action
->setEnabled(false);
77 delete_action
->setEnabled(false);
79 if (!s
.multi_file_torrent
)
81 open_action
->setEnabled(true);
82 preview_path
= m_tc
->getStats().output_path
;
88 open_action
->setEnabled(true);
89 preview_path
= file
->getPathOnDisk();
91 download_first_action
->setEnabled(file
->getPriority() != FIRST_PRIORITY
);
92 download_normal_action
->setEnabled(file
->getPriority() != NORMAL_PRIORITY
);
93 download_last_action
->setEnabled(file
->getPriority() != LAST_PRIORITY
);
94 dnd_action
->setEnabled(file
->getPriority() != ONLY_SEED_PRIORITY
);
95 delete_action
->setEnabled(file
->getPriority() != EXCLUDED
);
99 open_action
->setEnabled(false);
104 download_first_action
->setEnabled(true);
105 download_normal_action
->setEnabled(true);
106 download_last_action
->setEnabled(true);
107 dnd_action
->setEnabled(true);
108 delete_action
->setEnabled(true);
109 open_action
->setEnabled(true);
110 preview_path
= m_tc
->getDataDir() + fileTreeModel
->dirPath(item
);
113 contextMenu
->popup(QCursor::pos());
116 void BTFileTreeView::open()
118 new KRun(KUrl(preview_path
), 0, false, true);
121 void BTFileTreeView::changePriority(bt::Priority newpriority
)
123 fileTreeModel
->changePriority(selectionModel()->selectedRows(2),newpriority
);
126 void BTFileTreeView::downloadFirst()
128 changePriority(FIRST_PRIORITY
);
131 void BTFileTreeView::downloadLast()
133 changePriority(LAST_PRIORITY
);
136 void BTFileTreeView::downloadNormal()
138 changePriority(NORMAL_PRIORITY
);
141 void BTFileTreeView::doNotDownload()
143 changePriority(ONLY_SEED_PRIORITY
);
146 void BTFileTreeView::deleteFiles()
148 QModelIndexList sel
= selectionModel()->selectedRows();
149 Uint32 n
= sel
.count();
150 if (n
== 1) // single item can be a directory
152 if (!fileTreeModel
->indexToFile(sel
.front()))
156 QString msg
= n
> 1 ? i18n("You will lose all data in this file, are you sure you want to do this ?") :
157 i18n("You will lose all data in these files, are you sure you want to do this ?");
159 if (KMessageBox::warningYesNo(0,msg
) == KMessageBox::Yes
)
160 changePriority(EXCLUDED
);