A few deprecations eradicated involving QCheckBox.
[kdenetwork.git] / filesharing / advanced / kcm_sambaconf / hiddenfileview.cpp
blobf107e9352284b07e1ee601b887088d6fd35bd0bb
1 /***************************************************************************
2 hiddenfileview.cpp - description
3 -------------------
4 begin : Wed Jan 1 2003
5 copyright : (C) 2003 by Jan Sch�er
6 email : janschaefer@users.sourceforge.net
7 ***************************************************************************/
9 /******************************************************************************
10 * *
11 * This file is part of KSambaPlugin. *
12 * *
13 * KSambaPlugin is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * KSambaPlugin is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with KSambaPlugin; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
26 * *
27 ******************************************************************************/
29 #include <assert.h>
31 #include <qcheckbox.h>
32 #include <qlineedit.h>
33 #include <qregexp.h>
34 #include <qstringlist.h>
35 #include <q3groupbox.h>
36 #include <qcursor.h>
37 //Added by qt3to4:
38 #include <Q3PtrList>
41 #include <kmenu.h>
42 #include <kaction.h>
43 #include <krestrictedline.h>
44 #include <klocale.h>
45 #include <kdirlister.h>
46 #include <kmessagebox.h>
47 #include <kurlrequester.h>
48 #include <ktoggleaction.h>
49 #include <kdebug.h>
51 #include "hiddenfileview.h"
52 #include "sharedlgimpl.h"
53 #include "sambashare.h"
55 #define DEBUG 5009
57 #define COL_NAME 0
58 #define COL_HIDDEN 1
59 #define COL_VETO 2
60 #define COL_VETO_OPLOCK 3
61 #define COL_SIZE 4
62 #define COL_DATE 5
63 #define COL_PERM 6
64 #define COL_OWNER 7
65 #define COL_GROUP 8
67 #define HIDDENTABINDEX 5
69 HiddenListViewItem::HiddenListViewItem( Q3ListView *parent, const KFileItem &fi, bool hidden=false, bool veto=false, bool vetoOplock=false )
70 : QMultiCheckListItem( parent )
72 setPixmap( COL_NAME, fi.pixmap(KIconLoader::SizeSmall));
74 setText( COL_NAME, fi.text() );
75 setText( COL_SIZE, KGlobal::locale()->formatNumber( fi.size(), 0));
76 setText( COL_DATE, fi.timeString() );
77 setText( COL_PERM, fi.permissionsString() );
78 setText( COL_OWNER, fi.user() );
79 setText( COL_GROUP, fi.group() );
81 setOn(COL_HIDDEN,hidden);
82 setOn(COL_VETO,veto);
83 setOn(COL_VETO_OPLOCK,vetoOplock);
85 _fileItem = fi;
88 HiddenListViewItem::~HiddenListViewItem()
92 KFileItem HiddenListViewItem::getFileItem() const
94 return _fileItem;
98 void HiddenListViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
100 QColorGroup _cg = cg;
102 if (isOn(COL_VETO))
103 _cg.setColor(QPalette::Base,Qt::lightGray);
105 if (isOn(COL_HIDDEN))
106 _cg.setColor(QPalette::Text,Qt::gray);
108 QMultiCheckListItem::paintCell(p, _cg, column, width, alignment);
114 HiddenFileView::HiddenFileView(ShareDlgImpl* shareDlg, SambaShare* share)
116 _share = share;
117 _dlg = shareDlg;
119 _hiddenActn = new KToggleAction(i18n("&Hide"), this);
120 _vetoActn = new KToggleAction(i18n("&Veto"), this);
121 _vetoOplockActn = new KToggleAction(i18n("&Veto Oplock"), this);
123 initListView();
125 _dlg->hiddenChk->setTristate(true);
126 _dlg->vetoChk->setTristate(true);
128 connect( _dlg->hiddenChk, SIGNAL(toggled(bool)), this, SLOT( hiddenChkClicked(bool) ));
129 connect( _dlg->vetoChk, SIGNAL(toggled(bool)), this, SLOT( vetoChkClicked(bool) ));
130 connect( _dlg->vetoOplockChk, SIGNAL(toggled(bool)), this, SLOT( vetoOplockChkClicked(bool) ));
132 _dlg->hiddenEdit->setText( _share->getValue("hide files") );
133 connect( _dlg->hiddenEdit, SIGNAL(textChanged(const QString &)), this, SLOT(updateView()));
135 _dlg->vetoEdit->setText( _share->getValue("veto files") );
136 connect( _dlg->vetoEdit, SIGNAL(textChanged(const QString &)), this, SLOT(updateView()));
138 _dlg->vetoOplockEdit->setText( _share->getValue("veto oplock files") );
139 connect( _dlg->vetoOplockEdit, SIGNAL(textChanged(const QString &)), this, SLOT(updateView()));
141 // new QLabel(i18n("Hint")+" : ",grid);
142 // new QLabel(i18n("You have to separate the entries with a '/'. You can use the wildcards '*' and '?'"),grid);
143 // new QLabel(i18n("Example")+" : ",grid);
144 // new QLabel(i18n("*.tmp/*SECRET*/.*/file?.*/"),grid);
146 _dir = new KDirLister(this);
147 _dir->setShowingDotFiles(true);
149 connect( _dir, SIGNAL(newItems(const KFileItemList &)),
150 this, SLOT(insertNewFiles(const KFileItemList &)));
152 connect( _dir, SIGNAL(deleteItem(const KFileItem&)),
153 this, SLOT(deleteItem(const KFileItem&)));
155 connect( _dir, SIGNAL(refreshItems(const QList<QPair<KFileItem, KFileItem> > &)),
156 this, SLOT(refreshItems(const QList<QPair<KFileItem, KFileItem> > &)));
158 connect( _hiddenActn, SIGNAL(toggled(bool)), this, SLOT(hiddenChkClicked(bool)));
159 connect( _vetoActn, SIGNAL(toggled(bool)), this, SLOT(vetoChkClicked(bool)));
160 connect( _vetoOplockActn, SIGNAL(toggled(bool)), this, SLOT(vetoOplockChkClicked(bool)));
163 void HiddenFileView::initListView()
165 _dlg->hiddenListView->setMultiSelection(true);
166 _dlg->hiddenListView->setSelectionMode(Q3ListView::Extended);
167 _dlg->hiddenListView->setAllColumnsShowFocus(true);
169 _hiddenList = createRegExpList(_share->getValue("hide files"));
170 _vetoList = createRegExpList(_share->getValue("veto files"));
171 _vetoOplockList = createRegExpList(_share->getValue("veto oplock files"));
173 _popup = new KMenu(_dlg->hiddenListView);
175 _popup->addAction(_hiddenActn);
176 _popup->addAction(_vetoActn);
177 _popup->addAction(_vetoOplockActn);
179 connect( _dlg->hiddenListView, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
180 connect( _dlg->hiddenListView, SIGNAL(contextMenu(K3ListView*,Q3ListViewItem*,const QPoint&)),
181 this, SLOT(showContextMenu()));
183 connect( _dlg->hideDotFilesChk, SIGNAL(toggled(bool)), this, SLOT(hideDotFilesChkClicked(bool)));
184 connect( _dlg->hideUnreadableChk, SIGNAL(toggled(bool)), this, SLOT(hideUnreadableChkClicked(bool)));
186 connect( _dlg->hiddenListView, SIGNAL(mouseButtonPressed(int,Q3ListViewItem*,const QPoint &,int)),
187 this, SLOT(slotMouseButtonPressed(int,Q3ListViewItem*,const QPoint &,int)));
190 HiddenFileView::~HiddenFileView()
194 void HiddenFileView::load()
196 if (_dlg->hiddenListView)
197 _dlg->hiddenListView->clear();
199 _dir->openUrl( _dlg->pathUrlRq->url() );
202 void HiddenFileView::save()
204 QString s = _dlg->hiddenEdit->text().trimmed();
205 // it is important that the string ends with an '/'
206 // otherwise Samba won't recognize the last entry
207 if ( (!s.isEmpty()) && (s.right(1)!="/"))
208 s+='/';
209 _share->setValue("hide files", s);
211 s = _dlg->vetoEdit->text().trimmed();
212 // it is important that the string ends with an '/'
213 // otherwise Samba won't recognize the last entry
214 if ( (!s.isEmpty()) && (s.right(1)!="/"))
215 s+='/';
216 _share->setValue("veto files", s);
218 s = _dlg->vetoOplockEdit->text().trimmed();
219 // it is important that the string ends with an '/'
220 // otherwise Samba won't recognize the last entry
221 if ( (!s.isEmpty()) && (s.right(1)!="/"))
222 s+='/';
223 _share->setValue("veto oplock files", s);
227 void HiddenFileView::insertNewFiles(const KFileItemList &newone)
229 if ( newone.isEmpty() )
230 return;
232 int j=0;
234 KFileItemList::const_iterator it = newone.begin();
235 const KFileItemList::const_iterator end = newone.end();
236 for ( ; it != end; ++it ) {
237 const KFileItem tmp = *it;
238 j++;
240 bool hidden = matchHidden(tmp.text());
241 bool veto = matchVeto(tmp.text());
242 bool vetoOplock = matchVetoOplock(tmp.text());
243 kDebug(DEBUG) << "creating new HiddenListViewItem " << tmp.text() << endl;
244 new HiddenListViewItem( _dlg->hiddenListView, tmp, hidden, veto, vetoOplock );
251 void HiddenFileView::columnClicked(int column) {
254 switch (column) {
255 case COL_HIDDEN : hiddenChkClicked( !_dlg->hiddenChk->isChecked() );break;
256 case COL_VETO : vetoChkClicked( !_dlg->vetoChk->isChecked() );break;
257 case COL_VETO_OPLOCK : vetoOplockChkClicked( !_dlg->vetoOplockChk->isChecked() );break;
258 default : break;
262 void HiddenFileView::deleteItem( const KFileItem &fileItem )
264 HiddenListViewItem* item;
265 for (item = dynamic_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
266 item = dynamic_cast<HiddenListViewItem*>(item->nextSibling()))
268 if (item->getFileItem() == fileItem)
270 delete item;
271 break;
277 void HiddenFileView::refreshItems( const QList<QPair<KFileItem, KFileItem> >& /*items*/ )
279 updateView();
283 void HiddenFileView::showContextMenu()
285 _popup->exec(QCursor::pos());
289 void HiddenFileView::selectionChanged()
291 bool veto = false;
292 bool noVeto = false;
293 bool hide = false;
294 bool noHide = false;
295 bool vetoOplock = false;
296 bool noVetoOplock = false;
298 int n = 0;
300 HiddenListViewItem* item;
301 for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
302 item = static_cast<HiddenListViewItem*>(item->nextSibling()))
304 if (!item->isSelected())
305 continue;
307 n++;
309 if (item->isOn(COL_VETO))
310 veto = true;
311 else
312 noVeto = true;
314 if (item->isOn(COL_VETO_OPLOCK))
315 vetoOplock = true;
316 else
317 noVetoOplock = true;
319 if (item->isOn(COL_HIDDEN))
320 hide = true;
321 else
322 noHide = true;
326 _dlg->selGrpBx->setEnabled(n>0);
328 if (veto && noVeto)
330 _dlg->vetoChk->setTristate(true);
331 _dlg->vetoChk->setCheckState(Qt::PartiallyChecked);
332 _dlg->vetoChk->update();
334 else
336 _dlg->vetoChk->setTristate(false);
337 _dlg->vetoChk->setChecked(veto);
340 if (vetoOplock && noVetoOplock)
342 _dlg->vetoOplockChk->setTristate(true);
343 _dlg->vetoOplockChk->setCheckState(Qt::PartiallyChecked);
344 _dlg->vetoOplockChk->update();
346 else
348 _dlg->vetoOplockChk->setTristate(false);
349 _dlg->vetoOplockChk->setChecked(vetoOplock);
353 if (hide && noHide)
355 _dlg->hiddenChk->setTristate(true);
356 _dlg->hiddenChk->setCheckState(Qt::PartiallyChecked);
357 _dlg->hiddenChk->update();
359 else
361 _dlg->hiddenChk->setTristate(false);
362 _dlg->hiddenChk->setChecked(hide);
366 void HiddenFileView::checkBoxClicked(QCheckBox* chkBox,KToggleAction* action,QLineEdit* edit, int column,Q3PtrList<QRegExp> & reqExpList,bool b) {
367 // We don't save the old state so
368 // disable the tristate mode
369 chkBox->setTristate(false);
370 action->setChecked(b);
371 chkBox->setChecked(b);
373 HiddenListViewItem* item;
374 for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
375 item = static_cast<HiddenListViewItem*>(item->nextSibling()))
377 if (!item->isSelected())
378 continue;
380 if (b == item->isOn(column))
381 continue;
383 if (!b) {
384 QRegExp* rx = getRegExpListMatch(item->text(0),reqExpList);
386 // Perhaps the file was hidden because it started with a dot
387 if (!rx && item->text(0)[0]=='.' && _dlg->hideDotFilesChk->isChecked()) {
388 int result = KMessageBox::questionYesNo(_dlg,i18n(
389 "<qt>Some files you have selected are hidden because they start with a dot; "
390 "do you want to uncheck all files starting with a dot?</qt>"),i18n("Files Starting With Dot"),KGuiItem(i18n("Uncheck Hidden")), KGuiItem(i18n("Keep Hidden")));
392 if (result == KMessageBox::No) {
393 Q3PtrList<HiddenListViewItem> lst = getMatchingItems(QRegExp(".*",false,true));
394 deselect(lst);
395 } else {
396 _dlg->hideDotFilesChk->setChecked(false);
398 continue;
399 } else {
400 if (rx) {
401 // perhaps it is matched by a wildcard string
402 QString p = rx->pattern();
403 if ( p.contains('*') ||
404 p.contains('?') )
406 // TODO after message freeze: why show three times the wildcard string? Once should be enough.
407 // TODO remove <b></b> and use <qt> instead
408 int result = KMessageBox::questionYesNo(_dlg,i18n(
409 "<b></b>Some files you have selected are matched by the wildcarded string <b>'%1'</b>; "
410 "do you want to uncheck all files matching <b>'%1'</b>?", rx->pattern()),
411 i18n("Wildcarded String"),KGuiItem(i18n("Uncheck Matches")),KGuiItem(i18n("Keep Selected")));
413 Q3PtrList<HiddenListViewItem> lst = getMatchingItems( *rx );
415 if (result == KMessageBox::No) {
416 deselect(lst);
417 } else {
418 setState(lst,column,false);
419 reqExpList.remove(rx);
420 updateEdit(edit, reqExpList);
422 continue;
423 } else {
424 reqExpList.remove(rx);
425 updateEdit(edit, reqExpList);
430 else {
431 reqExpList.append( new QRegExp(item->text(0)) );
432 updateEdit(edit, reqExpList);
435 item->setOn(column,b);
438 _dlg->hiddenListView->update();
441 void HiddenFileView::hiddenChkClicked(bool b)
443 checkBoxClicked(_dlg->hiddenChk, _hiddenActn, _dlg->hiddenEdit, COL_HIDDEN,_hiddenList,b);
447 void HiddenFileView::vetoOplockChkClicked(bool b) {
448 checkBoxClicked(_dlg->vetoOplockChk, _vetoOplockActn, _dlg->vetoOplockEdit, COL_VETO_OPLOCK,_vetoOplockList,b);
451 void HiddenFileView::vetoChkClicked(bool b)
453 checkBoxClicked(_dlg->vetoChk, _vetoActn, _dlg->vetoEdit, COL_VETO,_vetoList,b);
457 * Sets the text of the QLineEdit edit to the entries of the passed QRegExp-List
459 void HiddenFileView::updateEdit(QLineEdit* edit, Q3PtrList<QRegExp> & lst)
461 QString s="";
463 QRegExp* rx;
464 for(rx = static_cast<QRegExp*>(lst.first()); rx;
465 rx = static_cast<QRegExp*>(lst.next()) )
467 s+= rx->pattern()+QString("/");
470 edit->setText(s);
474 void HiddenFileView::setState(Q3PtrList<HiddenListViewItem> & lst, int column, bool b) {
475 HiddenListViewItem* item;
476 for(item = static_cast<HiddenListViewItem*>(lst.first()); item;
477 item = static_cast<HiddenListViewItem*>(lst.next()) )
479 item->setOn(column,b);
484 void HiddenFileView::deselect(Q3PtrList<HiddenListViewItem> & lst)
486 HiddenListViewItem* item;
487 for(item = static_cast<HiddenListViewItem*>(lst.first()); item;
488 item = static_cast<HiddenListViewItem*>(lst.next()) )
490 item->setSelected(false);
495 Q3PtrList<HiddenListViewItem> HiddenFileView::getMatchingItems(const QRegExp & rx)
497 Q3PtrList<HiddenListViewItem> lst;
499 HiddenListViewItem* item;
500 for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
501 item = static_cast<HiddenListViewItem*>(item->nextSibling()))
503 if (rx.exactMatch(item->text(0)))
504 lst.append(item);
507 return lst;
510 void HiddenFileView::updateView()
512 _hiddenList = createRegExpList(_dlg->hiddenEdit->text());
513 _vetoList = createRegExpList(_dlg->vetoEdit->text());
514 _vetoOplockList = createRegExpList(_dlg->vetoOplockEdit->text());
516 HiddenListViewItem* item;
517 for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
518 item = static_cast<HiddenListViewItem*>(item->nextSibling()))
520 item->setOn(COL_HIDDEN,matchHidden(item->text(0)));
521 item->setOn(COL_VETO,matchVeto(item->text(0)));
522 item->setOn(COL_VETO_OPLOCK,matchVetoOplock(item->text(0)));
525 _dlg->hiddenListView->repaint();
529 Q3PtrList<QRegExp> HiddenFileView::createRegExpList(const QString & s)
531 Q3PtrList<QRegExp> lst;
532 bool cs = _share->getBoolValue("case sensitive");
534 if (!s.isEmpty())
536 QStringList l = QStringList::split("/",s);
538 for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it ) {
539 lst.append( new QRegExp(*it,cs,true) );
543 return lst;
546 bool HiddenFileView::matchHidden(const QString & s)
548 Q3PtrList<QRegExp> hiddenList(_hiddenList);
550 if (_dlg->hideDotFilesChk->isChecked())
551 hiddenList.append( new QRegExp(".*",false,true) );
553 return matchRegExpList(s,hiddenList);
556 bool HiddenFileView::matchVeto(const QString & s)
558 return matchRegExpList(s,_vetoList);
561 bool HiddenFileView::matchVetoOplock(const QString & s)
563 return matchRegExpList(s,_vetoOplockList);
566 bool HiddenFileView::matchRegExpList(const QString & s, Q3PtrList<QRegExp> & lst)
568 if (getRegExpListMatch(s,lst))
569 return true;
570 else
571 return false;
575 QRegExp* HiddenFileView::getHiddenMatch(const QString & s)
577 return getRegExpListMatch(s,_hiddenList);
580 QRegExp* HiddenFileView::getVetoMatch(const QString & s)
582 return getRegExpListMatch(s,_vetoList);
585 QRegExp* HiddenFileView::getRegExpListMatch(const QString & s, Q3PtrList<QRegExp> & lst)
587 QRegExp* rx;
589 for ( rx = lst.first(); rx; rx = lst.next() )
591 if (rx->exactMatch(s))
592 return rx;
595 return 0L;
598 void HiddenFileView::hideDotFilesChkClicked(bool)
600 updateView();
603 void HiddenFileView::hideUnreadableChkClicked(bool)
605 updateView();
608 void HiddenFileView::slotMouseButtonPressed( int, Q3ListViewItem*, const QPoint&, int c ) {
609 columnClicked(c);
613 #include "hiddenfileview.moc"