use red, yellow, green to indicate not installed, outdated, up2date
[kfpm.git] / mainwidget.cpp
blob3e67d6a24fbd6a5ce5bc07eda40df2b569470696
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>
22 #include <qstrlist.h>
24 #include "kfpm.h"
25 #include "mainwidget.h"
27 extern QPtrList<PM_DB> syncs;
29 MainWidget::MainWidget(QWidget * parent, const char * name, WFlags f) : QWidget (parent, name, f)
31 QVBoxLayout *topLayout = new QVBoxLayout(this);
32 Q_CHECK_PTR(topLayout);
34 /* Menubar */
35 QPopupMenu *file = new QPopupMenu(this);
36 Q_CHECK_PTR(file);
37 file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q );
39 QPopupMenu *help = new QPopupMenu(this);
40 Q_CHECK_PTR(help);
41 help->insertItem( "&About", this, SLOT(about()) );
42 help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
44 QMenuBar *menubar = new QMenuBar(this);
45 Q_CHECK_PTR(menubar);
46 menubar->insertItem( "&File", file );
47 menubar->insertItem( "&Help", help);
49 topLayout->setMenuBar(menubar);
51 /* Groups */
52 GroupsView = new QListView(this);
53 Q_CHECK_PTR(GroupsView);
54 GroupsView->addColumn("Groups");
56 GroupsView->setSelectionMode( QListView::Single );
58 connect( GroupsView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(groupItemSelected(QListViewItem*)) );
60 /* Packages */
61 PackagesView = new QListView(this);
62 Q_CHECK_PTR(PackagesView);
63 PackagesView->addColumn("Name");
64 PackagesView->addColumn("Status");
65 PackagesView->addColumn("Version");
67 PackagesView->setSelectionMode( QListView::Single );
69 connect( PackagesView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(packageItemSelected(QListViewItem*)) );
71 /* Info */
72 InfoView = new QListView(this);
73 Q_CHECK_PTR(InfoView);
74 InfoView->header()->hide();
75 /* Disable sorting
76 InfoView->setSorting(-1);*/
77 InfoView->setSelectionMode(QListView::NoSelection);
78 InfoView->addColumn("Directive");
79 InfoView->addColumn("Value");
81 /* Details */
82 QTabWidget *pkgdetails = new QTabWidget(this);
83 Q_CHECK_PTR(pkgdetails);
84 pkgdetails->addTab(InfoView, "Info");
86 /* Layout */
87 QHBoxLayout *leftgrouplist = new QHBoxLayout(topLayout);
88 Q_CHECK_PTR(leftgrouplist);
89 leftgrouplist->addWidget(GroupsView);
91 QVBoxLayout *details = new QVBoxLayout(leftgrouplist);
92 Q_CHECK_PTR(details);
93 details->addWidget(PackagesView);
94 details->addWidget(pkgdetails);
96 /* StatusBar */
97 StatusBar = new QStatusBar(this);
98 Q_CHECK_PTR(StatusBar);
100 topLayout->addWidget(StatusBar);
103 MainWidget::~MainWidget()
105 delete FPMClass;
108 void MainWidget::about() {
109 QMessageBox::about(this, "Kfpm for Frugalware",
110 "Kfpm is a graphical fronted for libpacman.\n\n"
111 "(c) 2007 Vöröskõi András");
114 void MainWidget::aboutQt() {
115 QMessageBox::aboutQt(this, "About Qt");
118 void MainWidget::updateGroupList() {
119 QStrList groupnames = FPMClass->sync_group(FPMClass->getSync());
120 for (QStrList::Iterator it = groupnames.begin(); it != groupnames.end(); it++) {
121 (void) new QListViewItem(GroupsView, *it);
123 setStatusBarMessage("Grouplist loaded.");
126 void MainWidget::updateGroupPackageList(const char *groupname) {
127 QPixmap pm(12, 12);
128 pm.fill(Qt::green);
129 QStrList *foo = new QStrList();
130 foo->append(groupname);
131 QValueVector<PM_DB*> dbs = FPMClass->getSync();
132 QStrList pkgnames = FPMClass->sync_group(dbs, foo);
133 for (QStrList::Iterator it = pkgnames.begin(); it != pkgnames.end(); it++) {
134 /* FIXME it should check all sync db, not just the first one */
135 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), *it);
136 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, *it);
138 QString version = (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION);
140 QString date = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE);
141 if (date.isEmpty())
142 pm.fill(Qt::red);
143 else {
144 QString localversion = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_VERSION);
145 if (localversion != version)
146 pm.fill(Qt::yellow);
149 QCheckListItem *clitem = new QCheckListItem(PackagesView, *it, QCheckListItem::CheckBox);
150 clitem->setPixmap(1, pm);
151 clitem->setText(2, version);
155 void MainWidget::slot_initialize() {
156 FPMClass = new FPM();
157 setStatusBarMessage("Initializing...");
158 qApp->processEvents();
159 updateGroupList();
162 void MainWidget::setStatusBarMessage(QString message) {
163 StatusBar->message(message);
166 void MainWidget::groupItemSelected( QListViewItem *item )
168 if (item == 0) {
169 return;
171 else {
172 PackagesView->clear();
173 updateGroupPackageList(item->text(0));
177 void MainWidget::packageItemSelected(QListViewItem *item)
179 if (item == 0) {
180 return;
182 updateFullInfo(item->text(0));
185 void MainWidget::updateFullInfo(const char *pkgname)
187 if (pkgname == 0) {
188 return;
190 else {
191 InfoView->clear();
192 QValueVector<PM_DB*> dbs = FPMClass->getSync();
193 /* FIXME it should check all sync db, not just the first one */
194 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), (char *)pkgname);
195 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, (char *)pkgname);
196 char buffer[50]; //for converting sizes
197 (void) new QListViewItem(InfoView, "Name", (char *)pacman_pkg_getinfo(pkg, PM_PKG_NAME));
198 (void) new QListViewItem(InfoView, "Version", (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION));
199 long int size = (long int)pacman_pkg_getinfo(pkg, PM_PKG_SIZE);
200 sprintf(buffer, "%ld", size);
201 (void) new QListViewItem(InfoView, "Size (compressed)", buffer);
202 size = (long int)pacman_pkg_getinfo(localpkg, PM_PKG_SIZE);
203 sprintf(buffer, "%ld", size);
204 (void) new QListViewItem(InfoView, "Size (uncompressed)", buffer);
205 (void) new QListViewItem(InfoView, "Description", (char *)pacman_pkg_getinfo(pkg, PM_PKG_DESC));
206 QString date = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE);
207 switch((long)pacman_pkg_getinfo(localpkg, PM_PKG_REASON)) {
208 case PM_PKG_REASON_EXPLICIT:
209 if(date.isEmpty())
210 (void) new QListViewItem(InfoView, "Reason", "Not installed");
211 else
212 (void) new QListViewItem(InfoView, "Reason", "Explicitly installed");
213 break;
214 case PM_PKG_REASON_DEPEND:
215 (void) new QListViewItem(InfoView, "Reason", "Installed as a dependency for another package");
216 break;
217 default:
218 (void) new QListViewItem(InfoView, "Reason", "Unknown");
219 break;
221 if (!date.isEmpty())
222 (void) new QListViewItem(InfoView, "Install Date", date);
226 // vim: sw=4 ts=4 noet