cosmetics and some warning fixes
[kfpm.git] / mainwidget.cpp
blob9d8f60ba974fd589408c4fe84cf0e8d4d9f5e851
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>
21 #include <qstrlist.h>
23 #include "kfpm.h"
24 #include "mainwidget.h"
26 extern QPtrList<PM_DB> syncs;
28 MainWidget::MainWidget(QWidget * parent, const char * name, WFlags f) : QWidget (parent, name, f)
30 QVBoxLayout *topLayout = new QVBoxLayout(this);
31 Q_CHECK_PTR(topLayout);
33 /* Menubar */
34 QPopupMenu *file = new QPopupMenu(this);
35 Q_CHECK_PTR(file);
36 file->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q );
38 QPopupMenu *help = new QPopupMenu(this);
39 Q_CHECK_PTR(help);
40 help->insertItem( "&About", this, SLOT(about()) );
41 help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
43 QMenuBar *menubar = new QMenuBar(this);
44 Q_CHECK_PTR(menubar);
45 menubar->insertItem( "&File", file );
46 menubar->insertItem( "&Help", help);
48 topLayout->setMenuBar(menubar);
50 /* Groups */
51 GroupsView = new QListView(this);
52 Q_CHECK_PTR(GroupsView);
53 GroupsView->addColumn("Groups");
55 GroupsView->setSelectionMode( QListView::Single );
57 connect( GroupsView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(groupItemSelected(QListViewItem*)) );
59 /* Packages */
60 PackagesView = new QListView(this);
61 Q_CHECK_PTR(PackagesView);
62 PackagesView->addColumn("Name");
63 PackagesView->addColumn("Status");
64 PackagesView->addColumn("Desctiption");
66 PackagesView->setSelectionMode( QListView::Single );
68 connect( PackagesView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT(packageItemSelected(QListViewItem*)) );
70 /* Info */
71 InfoView = new QListView(this);
72 Q_CHECK_PTR(InfoView);
73 InfoView->addColumn("Directive");
74 InfoView->addColumn("Value");
76 /* Details */
77 QTabWidget *pkgdetails = new QTabWidget(this);
78 Q_CHECK_PTR(pkgdetails);
79 pkgdetails->addTab(InfoView, "Info");
81 /* Layout */
82 QHBoxLayout *leftgrouplist = new QHBoxLayout(topLayout);
83 Q_CHECK_PTR(leftgrouplist);
84 leftgrouplist->addWidget(GroupsView);
86 QVBoxLayout *details = new QVBoxLayout(leftgrouplist);
87 Q_CHECK_PTR(details);
88 details->addWidget(PackagesView);
89 details->addWidget(pkgdetails);
91 /* StatusBar */
92 StatusBar = new QStatusBar(this);
93 Q_CHECK_PTR(StatusBar);
95 topLayout->addWidget(StatusBar);
98 MainWidget::~MainWidget()
100 delete FPMClass;
103 void MainWidget::about() {
104 QMessageBox::about(this, "Kfpm for Frugalware",
105 "Kfpm is a graphical fronted for libpacman.\n");
108 void MainWidget::aboutQt() {
109 QMessageBox::aboutQt(this, "About Qt");
112 void MainWidget::updateGroupList() {
113 QStrList groupnames = FPMClass->sync_group(FPMClass->getSync());
114 for (QStrList::Iterator it = groupnames.begin(); it != groupnames.end(); it++) {
115 (void) new QListViewItem(GroupsView, *it);
117 setStatusBarMessage("Grouplist loaded.");
120 void MainWidget::updateGroupPackageList(const char *groupname) {
121 QStrList *foo = new QStrList();
122 foo->append(groupname);
123 QValueVector<PM_DB*> dbs = FPMClass->getSync();
124 QStrList pkgnames = FPMClass->sync_group(dbs, foo);
125 for (QStrList::Iterator it = pkgnames.begin(); it != pkgnames.end(); it++) {
126 // PM_PKG *pacman_db_readpkg(PM_DB *db, char *name);
127 /* FIXME it should check all sync db, not just the first one */
128 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), *it);
129 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, *it);
131 QString date = (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE);
132 if (date.isEmpty()) {
133 std::cout << "not installed" << std::endl;
136 QCheckListItem *clitem = new QCheckListItem(PackagesView, *it, QCheckListItem::CheckBox);
137 clitem->setText(1, (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE));
138 clitem->setText(2, (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION));
142 void MainWidget::slot_initialize() {
143 FPMClass = new FPM();
144 setStatusBarMessage("Initializing...");
145 qApp->processEvents();
146 updateGroupList();
149 void MainWidget::setStatusBarMessage(QString message) {
150 StatusBar->message(message);
153 void MainWidget::groupItemSelected( QListViewItem *item )
155 if (item == 0) {
156 return;
158 else {
159 PackagesView->clear();
160 updateGroupPackageList(item->text(0));
164 void MainWidget::packageItemSelected(QListViewItem *item)
166 if (item == 0) {
167 return;
169 updateFullInfo(item->text(0));
172 void MainWidget::updateFullInfo(const char *pkgname)
174 if (pkgname == 0) {
175 return;
177 else {
178 InfoView->clear();
179 QValueVector<PM_DB*> dbs = FPMClass->getSync();
180 /* FIXME it should check all sync db, not just the first one */
181 PM_PKG *pkg = pacman_db_readpkg(dbs.first(), (char *)pkgname);
182 PM_PKG *localpkg = pacman_db_readpkg(FPMClass->LocalDB, (char *)pkgname);
183 char buffer[50]; //for converting sizes
184 (void) new QListViewItem(InfoView, "Name", (char *)pacman_pkg_getinfo(pkg, PM_PKG_NAME));
185 (void) new QListViewItem(InfoView, "Version", (char *)pacman_pkg_getinfo(pkg, PM_PKG_VERSION));
186 long int size = (long int)pacman_pkg_getinfo(pkg, PM_PKG_SIZE);
187 sprintf(buffer, "%ld", size);
188 (void) new QListViewItem(InfoView, "Size (compressed)", buffer);
189 (void) new QListViewItem(InfoView, "Description", (char *)pacman_pkg_getinfo(pkg, PM_PKG_DESC));
190 /* FIXME Says explicitly installed on uninstalled packages */
191 switch((long)pacman_pkg_getinfo(localpkg, PM_PKG_REASON)) {
192 case PM_PKG_REASON_EXPLICIT:
193 (void) new QListViewItem(InfoView, "Reason", "Explicitly installed");
194 break;
195 case PM_PKG_REASON_DEPEND:
196 (void) new QListViewItem(InfoView, "Reason", "Installed as a dependency for another package");
197 break;
198 default:
199 (void) new QListViewItem(InfoView, "Reason", "Unknown");
200 break;
202 (void) new QListViewItem(InfoView, "Install Date", (char *)pacman_pkg_getinfo(localpkg, PM_PKG_INSTALLDATE));
206 // vim: sw=4 ts=4 noet