* move Qt 3 support to subdirs which need it
[kdenetwork.git] / kppp / acctselect.cpp
blob07f7146a825d0ef4c9356ef5e074d24372616986
1 //---------------------------------------------------------------------------
2 //
3 // kPPP: A pppd front end for the KDE project
4 //
5 //---------------------------------------------------------------------------
6 //
7 // (c) 1997-1998 Bernd Johannes Wuebben <wuebben@kde.org>
8 // (c) 1997-1999 Mario Weilguni <mweilguni@kde.org>
9 // (c) 1998-1999 Harri Porten <porten@kde.org>
11 // derived from Jay Painters "ezppp"
13 //---------------------------------------------------------------------------
15 // $Id$
17 //---------------------------------------------------------------------------
19 // This program is free software; you can redistribute it and-or
20 // modify it under the terms of the GNU Library General Public
21 // License as published by the Free Software Foundation; either
22 // version 2 of the License, or (at your option) any later version.
24 // This program is distributed in the hope that it will be useful,
25 // but WITHOUT ANY WARRANTY; without even the implied warranty of
26 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 // Library General Public License for more details.
29 // You should have received a copy of the GNU Library General Public
30 // License along with this program; if not, write to the Free
31 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 //---------------------------------------------------------------------------
36 #include <qcombobox.h>
37 #include <qlabel.h>
38 //Added by qt3to4:
39 #include <QVBoxLayout>
40 #include <QFrame>
41 #include <QHBoxLayout>
42 #include <kurllabel.h>
43 #include <qlayout.h>
44 #include <q3listview.h>
45 #include <qdir.h>
46 #include <qregexp.h>
47 #include <qmatrix.h>
48 #include <qcheckbox.h>
49 #include <kdialog.h>
50 #include <kstandarddirs.h>
51 #include <klocale.h>
52 #include <kiconloader.h>
53 #include <krun.h>
55 #include "acctselect.h"
56 #include "pppdata.h"
59 AccountingSelector::AccountingSelector(QWidget *parent, bool _isnewaccount, const char *name)
60 : QWidget(parent),
61 isnewaccount(_isnewaccount)
63 setObjectName(name);
65 QVBoxLayout *l1 = new QVBoxLayout(parent);
66 l1->setSpacing(KDialog::spacingHint());
67 l1->setMargin(0);
69 enable_accounting = new QCheckBox(i18n("&Enable accounting"), parent);
70 l1->addWidget(enable_accounting, 1);
71 connect(enable_accounting, SIGNAL(toggled(bool)), this, SLOT(enableItems(bool)));
73 // insert the tree widget
74 tl = new Q3ListView(parent, "treewidget");
76 connect(tl, SIGNAL(selectionChanged(Q3ListViewItem*)), this,
77 SLOT(slotSelectionChanged(Q3ListViewItem*)));
78 tl->setMinimumSize(220, 200);
79 l1->addWidget(tl, 1);
81 KUrlLabel *up = new KUrlLabel(parent);
82 up->setText(i18n("Check for rule updates"));
83 up->setUrl("http://developer.kde.org/~kppp/rules.html");
84 connect(up, SIGNAL(leftClickedUrl(const QString&)), SLOT(openUrl(const QString&)));
86 l1->addWidget(up, 1);
88 // label to display the currently selected ruleset
89 QHBoxLayout *l11 = new QHBoxLayout;
90 l1->addSpacing(10);
91 l1->addLayout(l11);
92 QLabel *lsel = new QLabel(i18n("Selected:"), parent);
93 selected = new QLabel(parent);
94 selected->setFrameStyle(QFrame::Sunken | QFrame::WinPanel);
95 selected->setLineWidth(1);
96 selected->setFixedHeight(selected->sizeHint().height() + 16);
97 l11->addWidget(lsel, 0);
98 l11->addSpacing(10);
99 l11->addWidget(selected, 1);
101 // volume accounting
102 l1->addStretch(1);
103 QHBoxLayout *l12 = new QHBoxLayout;
104 l1->addLayout(l12);
105 QLabel *usevol_l = new QLabel(i18n("Volume accounting:"), parent);
106 use_vol = new QComboBox(parent);
107 use_vol->insertItem(0, i18n("No Accounting"));
108 use_vol->insertItem(1, i18n("Bytes In"));
109 use_vol->insertItem(2, i18n("Bytes Out"));
110 use_vol->insertItem(3, i18n("Bytes In & Out"));
111 use_vol->setCurrentIndex(gpppdata.VolAcctEnabled());
112 l12->addWidget(usevol_l);
113 l12->addWidget(use_vol);
115 // load the pmfolder pixmap from KDEdir
116 pmfolder = UserIcon("folder");
118 // scale the pixmap
119 if(pmfolder.width() > 0) {
120 QMatrix wm;
121 wm.scale(16.0/pmfolder.width(), 16.0/pmfolder.width());
122 pmfolder = pmfolder.transformed(wm);
125 // load the pmfolder pixmap from KDEdir
126 pmfile = UserIcon("phone");
128 // scale the pixmap
129 if(pmfile.width() > 0) {
130 QMatrix wm;
131 wm.scale(16.0/pmfile.width(), 16.0/pmfile.width());
132 pmfile = pmfile.transformed(wm);
135 enable_accounting->setChecked(gpppdata.AcctEnabled());
137 setupTreeWidget();
139 l1->activate();
143 QString AccountingSelector::fileNameToName(QString s) {
145 s.replace('_', " ");
146 return KUrl::decode_string(s);
151 QString AccountingSelector::nameToFileName(QString s) {
153 s.replace(' ', "_");
154 return s;
158 Q3ListViewItem *AccountingSelector::findByName(QString name)
160 Q3ListViewItem *ch = tl->firstChild();
161 while(ch) {
162 if(ch->text(0) == name)
163 return ch;
164 ch = ch->nextSibling();
166 return 0;
170 void AccountingSelector::insertDir(QDir d, Q3ListViewItem *root) {
172 Q3ListViewItem* tli = 0;
174 // sanity check
175 if(!d.exists() || !d.isReadable())
176 return;
179 // set up filter
180 QStringList filters;
181 filters << "*.rst";
182 d.setNameFilters(filters);
183 d.setFilter(QDir::Files);
184 d.setSorting(QDir::Name);
186 // read the list of files
187 const QFileInfoList list = d.entryInfoList();
188 QFileInfoList::const_iterator it = list.begin();
189 QFileInfoList::const_iterator itEnd = list.end();
190 QFileInfo fi;
192 // traverse the list and insert into the widget
193 while(it != itEnd) {
194 fi = *it;
196 QString samename = fi.fileName();
198 Q3ListViewItem *i = findByName(samename);
200 // skip this file if already in tree
201 if(i)
202 continue;
204 // check if this is the file we should mark
205 QString name = fileNameToName(fi.completeBaseName());
206 if(root)
207 tli = new Q3ListViewItem(root, name);
208 else
209 tli = new Q3ListViewItem(tl, name);
211 tli->setPixmap(0, pmfile);
213 // check if this is the item we are searching for
214 // (only in "Edit"-mode, not in "New"-mode
215 if(!isnewaccount && !edit_s.isEmpty() &&
216 (edit_s == QString(fi.filePath()).right(edit_s.length()))) {
217 edit_item = tli;
219 ++it;
222 // set up a filter for the directories
223 d.setFilter(QDir::Dirs);
224 filters.clear();
225 filters << "*";
226 d.setNameFilters(filters);
227 const QFileInfoList dlist = d.entryInfoList();
228 QFileInfoList::const_iterator dit= dlist.begin();
229 QFileInfoList::const_iterator ditEnd = dlist.end();
231 while(dit != ditEnd) {
232 fi = *dit;
233 // skip "." and ".." directories
234 if(fi.fileName().left(1) != ".") {
235 // convert to human-readable name
236 QString name = fileNameToName(fi.fileName());
238 // if the tree already has an item with this name,
239 // skip creation and use this one, otherwise
240 // create a new entry
241 Q3ListViewItem *i = findByName(name);
242 if(!i) {
243 Q3ListViewItem* item;
245 if(root)
246 item = new Q3ListViewItem(root, name);
247 else
248 item = new Q3ListViewItem(tl, name);
250 item->setPixmap(0, pmfolder);
252 insertDir(QDir(fi.filePath()), item);
253 } else
254 insertDir(QDir(fi.filePath()), i);
256 ++dit;
261 void AccountingSelector::setupTreeWidget() {
262 // search the item
263 edit_item = 0;
264 if(!isnewaccount) {
265 edit_s = gpppdata.accountingFile();
267 else
268 edit_s = "";
270 tl->addColumn( i18n("Available Rules") );
271 tl->setColumnWidth(0, 205);
272 tl->setRootIsDecorated(true);
274 // look in ~/.kde/share/apps/kppp/Rules and $KDEDIR/share/apps/kppp/Rules
275 QStringList dirs = KGlobal::dirs()->resourceDirs("appdata");
276 for (QStringList::ConstIterator it = dirs.begin();
277 it != dirs.end(); it++) {
278 insertDir(QDir((*it) + "Rules"), 0);
281 // when mode is "Edit", then hightlight the
282 // appropriate item
283 if(!isnewaccount && edit_item) {
284 tl->setSelected(edit_item, true);
285 tl->setOpen(edit_item->parent(), true);
286 tl->ensureItemVisible(edit_item);
289 enableItems(enable_accounting->isChecked());
293 void AccountingSelector::enableItems(bool enabled) {
295 tl->setEnabled(enabled);
297 if(!enabled || (!tl->currentItem()))
298 selected->setText(i18n("(none)"));
299 else {
300 Q3ListViewItem* i = tl->currentItem();
301 QString s;
302 while(i) {
303 s = '/' + i->text(0) + s;
304 i = i->parent();
306 selected->setText(s.mid(1));
308 s += ".rst";
309 edit_s = nameToFileName(s);
314 void AccountingSelector::slotSelectionChanged(Q3ListViewItem* i) {
316 if(!i || i->childCount())
317 return;
319 if(!enable_accounting->isChecked())
320 return;
322 enableItems(true);
326 bool AccountingSelector::save() {
328 if(enable_accounting->isChecked()) {
329 gpppdata.setAccountingFile(edit_s);
330 gpppdata.setAcctEnabled(true);
331 } else {
332 gpppdata.setAccountingFile("");
333 gpppdata.setAcctEnabled(false);
336 gpppdata.setVolAcctEnabled(use_vol->currentIndex());
338 return true;
341 void AccountingSelector::openUrl(const QString &url) {
342 new KRun( KUrl( url ),this);
345 #include "acctselect.moc"