1 //---------------------------------------------------------------------------
3 // kPPP: A pppd front end for the KDE project
5 //---------------------------------------------------------------------------
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 //---------------------------------------------------------------------------
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 <q3listview.h>
38 #include <qcombobox.h>
40 #include <kurllabel.h>
45 #include <qcheckbox.h>
47 #include <kstandarddirs.h>
49 #include <kiconloader.h>
52 #include "acctselect.h"
56 AccountingSelector::AccountingSelector(QWidget
*parent
, bool _isnewaccount
, const char *name
)
58 isnewaccount(_isnewaccount
)
62 QVBoxLayout
*l1
= new QVBoxLayout(parent
);
63 l1
->setSpacing(KDialog::spacingHint());
66 enable_accounting
= new QCheckBox(i18n("&Enable accounting"), parent
);
67 l1
->addWidget(enable_accounting
, 1);
68 connect(enable_accounting
, SIGNAL(toggled(bool)), this, SLOT(enableItems(bool)));
70 // insert the tree widget
71 tl
= new Q3ListView(parent
, "treewidget");
73 connect(tl
, SIGNAL(selectionChanged(Q3ListViewItem
*)), this,
74 SLOT(slotSelectionChanged(Q3ListViewItem
*)));
75 tl
->setMinimumSize(220, 200);
78 KUrlLabel
*up
= new KUrlLabel(parent
);
79 up
->setText(i18n("Check for rule updates"));
80 up
->setUrl("http://developer.kde.org/~kppp/rules.html");
81 connect(up
, SIGNAL(leftClickedUrl(const QString
&)), SLOT(openUrl(const QString
&)));
85 // label to display the currently selected ruleset
86 QHBoxLayout
*l11
= new QHBoxLayout
;
89 QLabel
*lsel
= new QLabel(i18n("Selected:"), parent
);
90 selected
= new QLabel(parent
);
91 selected
->setFrameStyle(QFrame::Sunken
| QFrame::WinPanel
);
92 selected
->setLineWidth(1);
93 selected
->setFixedHeight(selected
->sizeHint().height() + 16);
94 l11
->addWidget(lsel
, 0);
96 l11
->addWidget(selected
, 1);
100 QHBoxLayout
*l12
= new QHBoxLayout
;
102 QLabel
*usevol_l
= new QLabel(i18n("Volume accounting:"), parent
);
103 use_vol
= new QComboBox(parent
);
104 use_vol
->insertItem(0, i18n("No Accounting"));
105 use_vol
->insertItem(1, i18n("Bytes In"));
106 use_vol
->insertItem(2, i18n("Bytes Out"));
107 use_vol
->insertItem(3, i18n("Bytes In & Out"));
108 use_vol
->setCurrentIndex(gpppdata
.VolAcctEnabled());
109 l12
->addWidget(usevol_l
);
110 l12
->addWidget(use_vol
);
112 // load the pmfolder pixmap from KDEdir
113 pmfolder
= UserIcon("folder");
116 if(pmfolder
.width() > 0) {
118 wm
.scale(16.0/pmfolder
.width(), 16.0/pmfolder
.width());
119 pmfolder
= pmfolder
.transformed(wm
);
122 // load the pmfolder pixmap from KDEdir
123 pmfile
= UserIcon("phone");
126 if(pmfile
.width() > 0) {
128 wm
.scale(16.0/pmfile
.width(), 16.0/pmfile
.width());
129 pmfile
= pmfile
.transformed(wm
);
132 enable_accounting
->setChecked(gpppdata
.AcctEnabled());
140 QString
AccountingSelector::fileNameToName(QString s
) {
143 return KUrl::decode_string(s
);
148 QString
AccountingSelector::nameToFileName(QString s
) {
155 Q3ListViewItem
*AccountingSelector::findByName(QString name
)
157 Q3ListViewItem
*ch
= tl
->firstChild();
159 if(ch
->text(0) == name
)
161 ch
= ch
->nextSibling();
167 void AccountingSelector::insertDir(QDir d
, Q3ListViewItem
*root
) {
169 Q3ListViewItem
* tli
= 0;
172 if(!d
.exists() || !d
.isReadable())
179 d
.setNameFilters(filters
);
180 d
.setFilter(QDir::Files
);
181 d
.setSorting(QDir::Name
);
183 // read the list of files
184 const QFileInfoList list
= d
.entryInfoList();
185 QFileInfoList::const_iterator it
= list
.begin();
186 QFileInfoList::const_iterator itEnd
= list
.end();
189 // traverse the list and insert into the widget
193 QString samename
= fi
.fileName();
195 Q3ListViewItem
*i
= findByName(samename
);
197 // skip this file if already in tree
201 // check if this is the file we should mark
202 QString name
= fileNameToName(fi
.completeBaseName());
204 tli
= new Q3ListViewItem(root
, name
);
206 tli
= new Q3ListViewItem(tl
, name
);
208 tli
->setPixmap(0, pmfile
);
210 // check if this is the item we are searching for
211 // (only in "Edit"-mode, not in "New"-mode
212 if(!isnewaccount
&& !edit_s
.isEmpty() &&
213 (edit_s
== QString(fi
.filePath()).right(edit_s
.length()))) {
219 // set up a filter for the directories
220 d
.setFilter(QDir::Dirs
);
223 d
.setNameFilters(filters
);
224 const QFileInfoList dlist
= d
.entryInfoList();
225 QFileInfoList::const_iterator dit
= dlist
.begin();
226 QFileInfoList::const_iterator ditEnd
= dlist
.end();
228 while(dit
!= ditEnd
) {
230 // skip "." and ".." directories
231 if(fi
.fileName().left(1) != ".") {
232 // convert to human-readable name
233 QString name
= fileNameToName(fi
.fileName());
235 // if the tree already has an item with this name,
236 // skip creation and use this one, otherwise
237 // create a new entry
238 Q3ListViewItem
*i
= findByName(name
);
240 Q3ListViewItem
* item
;
243 item
= new Q3ListViewItem(root
, name
);
245 item
= new Q3ListViewItem(tl
, name
);
247 item
->setPixmap(0, pmfolder
);
249 insertDir(QDir(fi
.filePath()), item
);
251 insertDir(QDir(fi
.filePath()), i
);
258 void AccountingSelector::setupTreeWidget() {
262 edit_s
= gpppdata
.accountingFile();
267 tl
->addColumn( i18n("Available Rules") );
268 tl
->setColumnWidth(0, 205);
269 tl
->setRootIsDecorated(true);
271 // look in ~/.kde/share/apps/kppp/Rules and $KDEDIR/share/apps/kppp/Rules
272 const QStringList dirs
= KGlobal::dirs()->resourceDirs("appdata");
273 for (QStringList::ConstIterator it
= dirs
.constBegin();
274 it
!= dirs
.constEnd(); it
++) {
275 insertDir(QDir((*it
) + "Rules"), 0);
278 // when mode is "Edit", then hightlight the
280 if(!isnewaccount
&& edit_item
) {
281 tl
->setSelected(edit_item
, true);
282 tl
->setOpen(edit_item
->parent(), true);
283 tl
->ensureItemVisible(edit_item
);
286 enableItems(enable_accounting
->isChecked());
290 void AccountingSelector::enableItems(bool enabled
) {
292 tl
->setEnabled(enabled
);
294 if(!enabled
|| (!tl
->currentItem()))
295 selected
->setText(i18n("(none)"));
297 Q3ListViewItem
* i
= tl
->currentItem();
300 s
= '/' + i
->text(0) + s
;
303 selected
->setText(s
.mid(1));
306 edit_s
= nameToFileName(s
);
311 void AccountingSelector::slotSelectionChanged(Q3ListViewItem
* i
) {
313 if(!i
|| i
->childCount())
316 if(!enable_accounting
->isChecked())
323 bool AccountingSelector::save() {
325 if(enable_accounting
->isChecked()) {
326 gpppdata
.setAccountingFile(edit_s
);
327 gpppdata
.setAcctEnabled(true);
329 gpppdata
.setAccountingFile("");
330 gpppdata
.setAcctEnabled(false);
333 gpppdata
.setVolAcctEnabled(use_vol
->currentIndex());
338 void AccountingSelector::openUrl(const QString
&url
) {
339 new KRun( KUrl( url
),this);
342 #include "acctselect.moc"