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 <qcombobox.h>
39 #include <QVBoxLayout>
41 #include <QHBoxLayout>
42 #include <kurllabel.h>
44 #include <q3listview.h>
48 #include <qcheckbox.h>
50 #include <kstandarddirs.h>
52 #include <kiconloader.h>
55 #include "acctselect.h"
59 AccountingSelector::AccountingSelector(QWidget
*parent
, bool _isnewaccount
, const char *name
)
61 isnewaccount(_isnewaccount
)
65 QVBoxLayout
*l1
= new QVBoxLayout(parent
);
66 l1
->setSpacing(KDialog::spacingHint());
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);
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
&)));
88 // label to display the currently selected ruleset
89 QHBoxLayout
*l11
= new QHBoxLayout
;
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);
99 l11
->addWidget(selected
, 1);
103 QHBoxLayout
*l12
= new QHBoxLayout
;
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");
119 if(pmfolder
.width() > 0) {
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");
129 if(pmfile
.width() > 0) {
131 wm
.scale(16.0/pmfile
.width(), 16.0/pmfile
.width());
132 pmfile
= pmfile
.transformed(wm
);
135 enable_accounting
->setChecked(gpppdata
.AcctEnabled());
143 QString
AccountingSelector::fileNameToName(QString s
) {
146 return KUrl::decode_string(s
);
151 QString
AccountingSelector::nameToFileName(QString s
) {
158 Q3ListViewItem
*AccountingSelector::findByName(QString name
)
160 Q3ListViewItem
*ch
= tl
->firstChild();
162 if(ch
->text(0) == name
)
164 ch
= ch
->nextSibling();
170 void AccountingSelector::insertDir(QDir d
, Q3ListViewItem
*root
) {
172 Q3ListViewItem
* tli
= 0;
175 if(!d
.exists() || !d
.isReadable())
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();
192 // traverse the list and insert into the widget
196 QString samename
= fi
.fileName();
198 Q3ListViewItem
*i
= findByName(samename
);
200 // skip this file if already in tree
204 // check if this is the file we should mark
205 QString name
= fileNameToName(fi
.completeBaseName());
207 tli
= new Q3ListViewItem(root
, name
);
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()))) {
222 // set up a filter for the directories
223 d
.setFilter(QDir::Dirs
);
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
) {
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
);
243 Q3ListViewItem
* item
;
246 item
= new Q3ListViewItem(root
, name
);
248 item
= new Q3ListViewItem(tl
, name
);
250 item
->setPixmap(0, pmfolder
);
252 insertDir(QDir(fi
.filePath()), item
);
254 insertDir(QDir(fi
.filePath()), i
);
261 void AccountingSelector::setupTreeWidget() {
265 edit_s
= gpppdata
.accountingFile();
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
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)"));
300 Q3ListViewItem
* i
= tl
->currentItem();
303 s
= '/' + i
->text(0) + s
;
306 selected
->setText(s
.mid(1));
309 edit_s
= nameToFileName(s
);
314 void AccountingSelector::slotSelectionChanged(Q3ListViewItem
* i
) {
316 if(!i
|| i
->childCount())
319 if(!enable_accounting
->isChecked())
326 bool AccountingSelector::save() {
328 if(enable_accounting
->isChecked()) {
329 gpppdata
.setAccountingFile(edit_s
);
330 gpppdata
.setAcctEnabled(true);
332 gpppdata
.setAccountingFile("");
333 gpppdata
.setAcctEnabled(false);
336 gpppdata
.setVolAcctEnabled(use_vol
->currentIndex());
341 void AccountingSelector::openUrl(const QString
&url
) {
342 new KRun( KUrl( url
),this);
345 #include "acctselect.moc"