1 /***************************************************************************
2 hiddenfileview.cpp - description
5 copyright : (C) 2003 by Jan Sch�er
6 email : janschaefer@users.sourceforge.net
7 ***************************************************************************/
9 /******************************************************************************
11 * This file is part of KSambaPlugin. *
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. *
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. *
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 *
27 ******************************************************************************/
31 #include <qcheckbox.h>
32 #include <qlineedit.h>
34 #include <qstringlist.h>
35 #include <q3groupbox.h>
43 #include <krestrictedline.h>
45 #include <kdirlister.h>
46 #include <kmessagebox.h>
47 #include <kurlrequester.h>
48 #include <ktoggleaction.h>
51 #include "hiddenfileview.h"
52 #include "sharedlgimpl.h"
53 #include "sambashare.h"
60 #define COL_VETO_OPLOCK 3
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
);
83 setOn(COL_VETO_OPLOCK
,vetoOplock
);
88 HiddenListViewItem::~HiddenListViewItem()
92 KFileItem
HiddenListViewItem::getFileItem() const
98 void HiddenListViewItem::paintCell(QPainter
*p
, const QColorGroup
&cg
, int column
, int width
, int alignment
)
100 QColorGroup _cg
= cg
;
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
)
119 _hiddenActn
= new KToggleAction(i18n("&Hide"), this);
120 _vetoActn
= new KToggleAction(i18n("&Veto"), this);
121 _vetoOplockActn
= new KToggleAction(i18n("&Veto Oplock"), this);
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)!="/"))
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)!="/"))
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)!="/"))
223 _share
->setValue("veto oplock files", s
);
227 void HiddenFileView::insertNewFiles(const KFileItemList
&newone
)
229 if ( newone
.isEmpty() )
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
;
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
) {
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;
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
)
277 void HiddenFileView::refreshItems( const QList
<QPair
<KFileItem
, KFileItem
> >& /*items*/ )
283 void HiddenFileView::showContextMenu()
285 _popup
->exec(QCursor::pos());
289 void HiddenFileView::selectionChanged()
295 bool vetoOplock
= false;
296 bool noVetoOplock
= false;
300 HiddenListViewItem
* item
;
301 for (item
= static_cast<HiddenListViewItem
*>(_dlg
->hiddenListView
->firstChild());item
;
302 item
= static_cast<HiddenListViewItem
*>(item
->nextSibling()))
304 if (!item
->isSelected())
309 if (item
->isOn(COL_VETO
))
314 if (item
->isOn(COL_VETO_OPLOCK
))
319 if (item
->isOn(COL_HIDDEN
))
326 _dlg
->selGrpBx
->setEnabled(n
>0);
330 _dlg
->vetoChk
->setTristate(true);
331 _dlg
->vetoChk
->setCheckState(Qt::PartiallyChecked
);
332 _dlg
->vetoChk
->update();
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();
348 _dlg
->vetoOplockChk
->setTristate(false);
349 _dlg
->vetoOplockChk
->setChecked(vetoOplock
);
355 _dlg
->hiddenChk
->setTristate(true);
356 _dlg
->hiddenChk
->setCheckState(Qt::PartiallyChecked
);
357 _dlg
->hiddenChk
->update();
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())
380 if (b
== item
->isOn(column
))
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));
396 _dlg
->hideDotFilesChk
->setChecked(false);
401 // perhaps it is matched by a wildcard string
402 QString p
= rx
->pattern();
403 if ( 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
) {
418 setState(lst
,column
,false);
419 reqExpList
.remove(rx
);
420 updateEdit(edit
, reqExpList
);
424 reqExpList
.remove(rx
);
425 updateEdit(edit
, reqExpList
);
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
)
464 for(rx
= static_cast<QRegExp
*>(lst
.first()); rx
;
465 rx
= static_cast<QRegExp
*>(lst
.next()) )
467 s
+= rx
->pattern()+QString("/");
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)))
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");
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) );
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
))
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
)
589 for ( rx
= lst
.first(); rx
; rx
= lst
.next() )
591 if (rx
->exactMatch(s
))
598 void HiddenFileView::hideDotFilesChkClicked(bool)
603 void HiddenFileView::hideUnreadableChkClicked(bool)
608 void HiddenFileView::slotMouseButtonPressed( int, Q3ListViewItem
*, const QPoint
&, int c
) {
613 #include "hiddenfileview.moc"