ditz import
[kfpm.git] / mainwidget.cpp
blobe800150c14b8987266b35ad1598ba17dcf2441a2
1 /*
2 * mainwidget.cpp
4 * Copyright (c) 2007 VOROSKOI Andras <voroskoi@frugalware.org>
6 * Copyright: See COPYING file that comes with this distribution.
8 */
10 #include <iostream>
11 #include <qlistview.h>
12 #include <qlayout.h>
13 #include <qtabwidget.h>
14 #include <qlistbox.h>
15 #include <qmenubar.h>
16 #include <qapplication.h>
17 #include <qmessagebox.h>
18 #include <qstatusbar.h>
19 #include <qtimer.h>
20 #include <qheader.h>
21 #include <qaction.h>
23 #include <qstrlist.h>
25 #include "kfpm.h"
26 #include "mainwidget.h"
28 extern QPtrList<PM_DB> syncs;
30 MainWidget::MainWidget(QWidget * parent, const char * name, WFlags f) : QWidget (parent, name, f)
32 QVBoxLayout *topLayout = new QVBoxLayout(this);
33 Q_CHECK_PTR(topLayout);
35 /* Menubar */
36 QPopupMenu *file = new QPopupMenu(this);
37 Q_CHECK_PTR(file);
39 QAction *ExitAction = new QAction("E&xit", CTRL+Key_X, this, "exit");
40 connect(ExitAction, SIGNAL(activated()) , qApp, SLOT(quit()) );
41 ExitAction->addTo(file);
43 QPopupMenu *pacman = new QPopupMenu(this);
44 Q_CHECK_PTR(pacman);
45 QAction *UpdateAction = new QAction("&Update syncdb", CTRL+Key_Y, this, "foo");
46 connect(UpdateAction, SIGNAL(activated()) , this, SLOT(updateSyncdb()) );
47 UpdateAction->addTo(pacman);
48 QAction *UpgradeAction = new QAction("&Upgrade packages", CTRL+Key_U, this, "foo");
49 connect(UpgradeAction, SIGNAL(activated()) , this, SLOT(upgradePackages()) );
50 UpgradeAction->addTo(pacman);
52 QPopupMenu *help = new QPopupMenu(this);
53 Q_CHECK_PTR(help);
55 QAction *AboutAction = new QAction("&About", NULL, this, "about");
56 connect(AboutAction, SIGNAL(activated()), this, SLOT(about()) );
57 QAction *AboutQtAction = new QAction("About &Qt", NULL, this, "aboutQt");
58 connect(AboutQtAction, SIGNAL(activated()), this, SLOT(aboutQt()) );
59 AboutAction->addTo(help);
60 AboutQtAction->addTo(help);
62 QMenuBar *menubar = new QMenuBar(this);
63 Q_CHECK_PTR(menubar);
64 menubar->insertItem( "&File", file );
65 menubar->insertItem( "&Pacman", pacman );
66 menubar->insertItem( "&Help", help);
68 topLayout->setMenuBar(menubar);
70 /* Groups */
71 GroupsView = new QListView(this);
72 Q_CHECK_PTR(GroupsView);
73 GroupsView->addColumn("Groups");
75 GroupsView->setSelectionMode( QListView::Single );
77 connect( GroupsView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(groupItemSelected(QListViewItem*)) );
79 /* Packages */
80 PackagesView = new QListView(this);
81 Q_CHECK_PTR(PackagesView);
82 PackagesView->addColumn("Name");
83 PackagesView->addColumn("Status");
84 PackagesView->addColumn("Version");
86 PackagesView->setSelectionMode( QListView::Single );
88 connect( PackagesView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(packageItemSelected(QListViewItem*)) );
90 /* Info */
91 InfoView = new QListView(this);
92 Q_CHECK_PTR(InfoView);
93 InfoView->header()->hide();
94 /* Disable sorting
95 InfoView->setSorting(-1);*/
96 InfoView->setSelectionMode(QListView::NoSelection);
97 InfoView->addColumn("Directive");
98 InfoView->addColumn("Value");
100 /* Details */
101 QTabWidget *pkgdetails = new QTabWidget(this);
102 Q_CHECK_PTR(pkgdetails);
103 pkgdetails->addTab(InfoView, "Info");
105 /* Layout */
106 QHBoxLayout *leftgrouplist = new QHBoxLayout(topLayout);
107 Q_CHECK_PTR(leftgrouplist);
108 leftgrouplist->addWidget(GroupsView);
110 QVBoxLayout *details = new QVBoxLayout(leftgrouplist);
111 Q_CHECK_PTR(details);
112 details->addWidget(PackagesView);
113 details->addWidget(pkgdetails);
115 /* StatusBar */
116 StatusBar = new QStatusBar(this);
117 Q_CHECK_PTR(StatusBar);
119 topLayout->addWidget(StatusBar);
122 MainWidget::~MainWidget()
124 delete FPMClass;
127 void MainWidget::about() {
128 QMessageBox::about(this, "Kfpm for Frugalware",
129 "Kfpm is a graphical fronted for libpacman.\n\n"
130 "(c) 2007 Vöröskõi András");
133 void MainWidget::aboutQt() {
134 QMessageBox::aboutQt(this, "About Qt");
137 void MainWidget::updateGroupList() {
138 QStrList groupnames = FPMClass->sync_group(FPMClass->getSync());
139 for (QStrList::Iterator it = groupnames.begin(); it != groupnames.end(); it++) {
140 (void) new QListViewItem(GroupsView, *it);
142 setStatusBarMessage("Grouplist loaded.");
145 void MainWidget::updateGroupPackageList(const char *groupname) {
146 QPixmap pm(12, 12);
147 pm.fill(Qt::green);
148 QStrList *foo = new QStrList();
149 foo->append(groupname);
150 QValueVector<PM_DB*> dbs = FPMClass->getSync();
151 QStrList pkgnames = FPMClass->sync_group(dbs, foo);
152 for (QStrList::Iterator it = pkgnames.begin(); it != pkgnames.end(); it++) {
153 /* FIXME it should check all sync db, not just the first one */
154 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), *it);
155 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, *it);
157 QString version = (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION);
159 QString date = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE);
160 if (date.isEmpty())
161 pm.fill(Qt::red);
162 else {
163 QString localversion = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_VERSION);
164 if (localversion != version)
165 pm.fill(Qt::yellow);
168 QCheckListItem *clitem = new QCheckListItem(PackagesView, *it, QCheckListItem::CheckBox);
169 clitem->setPixmap(1, pm);
170 clitem->setText(2, version);
174 void MainWidget::slot_initialize() {
175 FPMClass = new FPM();
176 setStatusBarMessage("Initializing...");
177 qApp->processEvents();
178 updateGroupList();
181 void MainWidget::setStatusBarMessage(QString message) {
182 StatusBar->message(message);
185 void MainWidget::groupItemSelected( QListViewItem *item )
187 if (item == 0) {
188 return;
190 else {
191 PackagesView->clear();
192 updateGroupPackageList(item->text(0));
196 void MainWidget::packageItemSelected(QListViewItem *item)
198 if (item == 0) {
199 return;
201 updateFullInfo(item->text(0));
204 void MainWidget::updateFullInfo(const char *pkgname)
206 if (pkgname == 0) {
207 return;
209 else {
210 char buffer[50]; //for converting sizes
211 InfoView->clear();
212 QValueVector<PM_DB*> dbs = FPMClass->getSync();
214 /* FIXME it should check all sync db, not just the first one */
215 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), (char *)pkgname);
216 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, (char *)pkgname);
218 (void) new QListViewItem(InfoView, "Name", (char *)pacman_pkg_getinfo(pkg, PM_PKG_NAME));
220 (void) new QListViewItem(InfoView, "Version", (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION));
222 long int size = (long int)pacman_pkg_getinfo(pkg, PM_PKG_SIZE);
223 sprintf(buffer, "%ld", size);
224 (void) new QListViewItem(InfoView, "Size (compressed)", buffer);
226 size = (long int)pacman_pkg_getinfo(localpkg, PM_PKG_SIZE);
227 sprintf(buffer, "%ld", size);
228 (void) new QListViewItem(InfoView, "Size (uncompressed)", buffer);
230 (void) new QListViewItem(InfoView, "Description", (char *)pacman_pkg_getinfo(pkg, PM_PKG_DESC));
232 QString date = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE);
233 switch((long)pacman_pkg_getinfo(localpkg, PM_PKG_REASON)) {
234 case PM_PKG_REASON_EXPLICIT:
235 if(date.isEmpty())
236 (void) new QListViewItem(InfoView, "Reason", "Not installed");
237 else
238 (void) new QListViewItem(InfoView, "Reason", "Explicitly installed");
239 break;
240 case PM_PKG_REASON_DEPEND:
241 (void) new QListViewItem(InfoView, "Reason", "Installed as a dependency for another package");
242 break;
243 default:
244 (void) new QListViewItem(InfoView, "Reason", "Unknown");
245 break;
247 if (!date.isEmpty())
248 (void) new QListViewItem(InfoView, "Install Date", date);
250 PM_LIST *depends = (PM_LIST*)pacman_pkg_getinfo(pkg, PM_PKG_DEPENDS);
251 (void) new QListViewItem(InfoView, "Depends", FPMClass->PM_LISTtoQString(depends));
255 void MainWidget::updateSyncdb() {
256 QValueVector<PM_DB*> dbs = FPMClass->getSync();
257 // FIXME - catch expections
258 FPMClass->sync_synctree(0, dbs);
261 void MainWidget::upgradePackages() {
262 // FIXME -- exceptions
263 FPMClass->sync_upgrade();
266 // vim: sw=4 ts=4 noet