summaryrefslogtreecommitdiffstats
path: root/filesharing/advanced/kcm_sambaconf/hiddenfileview.cpp
blob: 2b629f42bd4730ae547f63ffb51df48644ef5bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/***************************************************************************
                          hiddenfileview.cpp  -  description
                             -------------------
    begin                : Wed Jan 1 2003
    copyright            : (C) 2003 by Jan Sch�er
    email                : janschaefer@users.sourceforge.net
 ***************************************************************************/

/******************************************************************************
 *                                                                            *
 *  This file is part of KSambaPlugin.                                          *
 *                                                                            *
 *  KSambaPlugin is free software; you can redistribute it and/or modify            *
 *  it under the terms of the GNU General Public License as published by      *
 *  the Free Software Foundation; either version 2 of the License, or         *
 *  (at your option) any later version.                                       *
 *                                                                            *
 *  KSambaPlugin is distributed in the hope that it will be useful,                 *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 *  GNU General Public License for more details.                              *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with KSambaPlugin; if not, write to the Free Software                     *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA  *
 *                                                                            *
 ******************************************************************************/

#include <assert.h>

#include <tqcheckbox.h>
#include <tqlineedit.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tqgroupbox.h>
#include <tqcursor.h>


#include <tdepopupmenu.h>
#include <tdeaction.h>
#include <krestrictedline.h>
#include <tdelocale.h>
#include <tdefiledetailview.h>
#include <kdirlister.h>
#include <tdemessagebox.h>
#include <kurlrequester.h>

#include "hiddenfileview.h"
#include "sharedlgimpl.h"
#include "sambashare.h"


#define COL_NAME 0
#define COL_HIDDEN 1
#define COL_VETO 2
#define COL_VETO_OPLOCK 3
#define COL_SIZE 4
#define COL_DATE 5
#define COL_PERM 6
#define COL_OWNER 7
#define COL_GROUP 8

#define HIDDENTABINDEX 5

HiddenListViewItem::HiddenListViewItem( TQListView *parent, KFileItem *fi, bool hidden=false, bool veto=false, bool vetoOplock=false )
  : QMultiCheckListItem( parent )
{
  setPixmap( COL_NAME, fi->pixmap(TDEIcon::SizeSmall));

  setText( COL_NAME, fi->text() );
  setText( COL_SIZE, TDEGlobal::locale()->formatNumber( fi->size(), 0));
  setText( COL_DATE,  fi->timeString() );
  setText( COL_PERM,  fi->permissionsString() );
  setText( COL_OWNER, fi->user() );
  setText( COL_GROUP, fi->group() );

  setOn(COL_HIDDEN,hidden);
  setOn(COL_VETO,veto);
  setOn(COL_VETO_OPLOCK,vetoOplock);

  _fileItem = fi;
}

HiddenListViewItem::~HiddenListViewItem()
{
}

KFileItem* HiddenListViewItem::getFileItem()
{
  return _fileItem;
}


void HiddenListViewItem::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int alignment)
{
  TQColorGroup _cg = cg;

  if (isOn(COL_VETO))
     _cg.setColor(TQColorGroup::Base,lightGray);

  if (isOn(COL_HIDDEN))
     _cg.setColor(TQColorGroup::Text,gray);

  QMultiCheckListItem::paintCell(p, _cg, column, width, alignment);
}




HiddenFileView::HiddenFileView(ShareDlgImpl* shareDlg, SambaShare* share)
{
  _share = share;
  _dlg = shareDlg;

  _hiddenActn = new TDEToggleAction(i18n("&Hide"));
  _vetoActn = new TDEToggleAction(i18n("&Veto"));
  _vetoOplockActn = new TDEToggleAction(i18n("&Veto Oplock"));

  initListView();

  _dlg->hiddenChk->setTristate(true);
  _dlg->vetoChk->setTristate(true);

  connect( _dlg->hiddenChk, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT( hiddenChkClicked(bool) ));
  connect( _dlg->vetoChk, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT( vetoChkClicked(bool) ));
  connect( _dlg->vetoOplockChk, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT( vetoOplockChkClicked(bool) ));

  _dlg->hiddenEdit->setText( _share->getValue("hide files") );
  connect( _dlg->hiddenEdit, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(updateView()));

  _dlg->vetoEdit->setText( _share->getValue("veto files") );
  connect( _dlg->vetoEdit, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(updateView()));

  _dlg->vetoOplockEdit->setText( _share->getValue("veto oplock files") );
  connect( _dlg->vetoOplockEdit, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(updateView()));

//  new TQLabel(i18n("Hint")+" : ",grid);
//  new TQLabel(i18n("You have to separate the entries with a '/'. You can use the wildcards '*' and '?'"),grid);
//  new TQLabel(i18n("Example")+" : ",grid);
//  new TQLabel(i18n("*.tmp/*SECRET*/.*/file?.*/"),grid);

  _dir = new KDirLister(true);
  _dir->setShowingDotFiles(true);

  connect( _dir, TQT_SIGNAL(newItems(const KFileItemList &)),
           this, TQT_SLOT(insertNewFiles(const KFileItemList &)));

  connect( _dir, TQT_SIGNAL(deleteItem(KFileItem*)),
           this, TQT_SLOT(deleteItem(KFileItem*)));

  connect( _dir, TQT_SIGNAL(refreshItems(const KFileItemList &)),
           this, TQT_SLOT(refreshItems(const KFileItemList &)));

  connect( _hiddenActn, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(hiddenChkClicked(bool)));
  connect( _vetoActn, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(vetoChkClicked(bool)));
  connect( _vetoOplockActn, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(vetoOplockChkClicked(bool)));
}

void HiddenFileView::initListView()
{
  _dlg->hiddenListView->setMultiSelection(true);
  _dlg->hiddenListView->setSelectionMode(TQListView::Extended);
  _dlg->hiddenListView->setAllColumnsShowFocus(true);

  _hiddenList = createRegExpList(_share->getValue("hide files"));
  _vetoList = createRegExpList(_share->getValue("veto files"));
  _vetoOplockList = createRegExpList(_share->getValue("veto oplock files"));

  _popup = new TDEPopupMenu(_dlg->hiddenListView);

  _hiddenActn->plug(_popup);
  _vetoActn->plug(_popup);
  _vetoOplockActn->plug(_popup);

  connect( _dlg->hiddenListView, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(selectionChanged()));
  connect( _dlg->hiddenListView, TQT_SIGNAL(contextMenu(TDEListView*,TQListViewItem*,const TQPoint&)),
           this, TQT_SLOT(showContextMenu()));

  connect( _dlg->hideDotFilesChk, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(hideDotFilesChkClicked(bool)));
  connect( _dlg->hideUnreadableChk, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(hideUnreadableChkClicked(bool)));

  connect( _dlg->hiddenListView, TQT_SIGNAL(mouseButtonPressed(int,TQListViewItem*,const TQPoint &,int)),
           this, TQT_SLOT(slotMouseButtonPressed(int,TQListViewItem*,const TQPoint &,int)));
}

HiddenFileView::~HiddenFileView()
{
    delete _dir;
}

void HiddenFileView::load()
{
  if (_dlg->hiddenListView)
    _dlg->hiddenListView->clear();

  _dir->openURL( _dlg->pathUrlRq->url() );
}

void HiddenFileView::save()
{
  TQString s = _dlg->hiddenEdit->text().stripWhiteSpace();
  // its important that the string ends with an '/'
  // otherwise Samba won't recognize the last entry
  if ( (!s.isEmpty()) && (s.right(1)!="/"))
      s+="/";
  _share->setValue("hide files", s);

  s = _dlg->vetoEdit->text().stripWhiteSpace();
  // its important that the string ends with an '/'
  // otherwise Samba won't recognize the last entry
  if ( (!s.isEmpty()) && (s.right(1)!="/"))
      s+="/";
  _share->setValue("veto files", s);

  s = _dlg->vetoOplockEdit->text().stripWhiteSpace();
  // its important that the string ends with an '/'
  // otherwise Samba won't recognize the last entry
  if ( (!s.isEmpty()) && (s.right(1)!="/"))
      s+="/";
  _share->setValue("veto oplock files", s);

}

void HiddenFileView::insertNewFiles(const KFileItemList &newone)
{
  if ( newone.isEmpty() )
     return;

  KFileItem *tmp;

  int j=0;

  for (KFileItemListIterator it(newone); (tmp = it.current()); ++it)
  {
    j++;

    bool hidden = matchHidden(tmp->text());
    bool veto = matchVeto(tmp->text());
    bool vetoOplock = matchVetoOplock(tmp->text());

    new HiddenListViewItem( _dlg->hiddenListView, tmp, hidden, veto, vetoOplock );

  }
}



void HiddenFileView::columnClicked(int column) {


  switch (column) {
    case COL_HIDDEN : hiddenChkClicked( !_dlg->hiddenChk->isOn() );break;
    case COL_VETO : vetoChkClicked( !_dlg->vetoChk->isOn() );break;
    case COL_VETO_OPLOCK : vetoOplockChkClicked( !_dlg->vetoOplockChk->isOn() );break;
    default : break;
  }
}

void HiddenFileView::deleteItem( KFileItem *fileItem )
{
  HiddenListViewItem* item;
  for (item = dynamic_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
       item = dynamic_cast<HiddenListViewItem*>(item->nextSibling()))
  {
    if (item->getFileItem() == fileItem)
    {
      delete item;
      break;
    }
  }

}

void HiddenFileView::refreshItems( const KFileItemList& /*items*/ )
{
  updateView();
}


void HiddenFileView::showContextMenu()
{
  _popup->exec(TQCursor::pos());
}


void HiddenFileView::selectionChanged()
{
  bool veto = false;
  bool noVeto = false;
  bool hide = false;
  bool noHide = false;
  bool vetoOplock = false;
  bool noVetoOplock = false;

  int n = 0;

  HiddenListViewItem* item;
  for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
       item = static_cast<HiddenListViewItem*>(item->nextSibling()))
  {
    if (!item->isSelected())
       continue;

    n++;

    if (item->isOn(COL_VETO))
       veto = true;
    else
       noVeto = true;

    if (item->isOn(COL_VETO_OPLOCK))
       vetoOplock = true;
    else
       noVetoOplock = true;

    if (item->isOn(COL_HIDDEN))
       hide = true;
    else
       noHide = true;
  }


  _dlg->selGrpBx->setEnabled(n>0);

  if (veto && noVeto)
  {
    _dlg->vetoChk->setTristate(true);
    _dlg->vetoChk->setNoChange();
    _dlg->vetoChk->update();
  }
  else
  {
    _dlg->vetoChk->setTristate(false);
    _dlg->vetoChk->setChecked(veto);
  }

  if (vetoOplock && noVetoOplock)
  {
    _dlg->vetoOplockChk->setTristate(true);
    _dlg->vetoOplockChk->setNoChange();
    _dlg->vetoOplockChk->update();
  }
  else
  {
    _dlg->vetoOplockChk->setTristate(false);
    _dlg->vetoOplockChk->setChecked(vetoOplock);
  }


  if (hide && noHide)
  {
    _dlg->hiddenChk->setTristate(true);
    _dlg->hiddenChk->setNoChange();
    _dlg->hiddenChk->update();
  }
  else
  {
    _dlg->hiddenChk->setTristate(false);
    _dlg->hiddenChk->setChecked(hide);
  }
}

void HiddenFileView::checkBoxClicked(TQCheckBox* chkBox,TDEToggleAction* action,TQLineEdit* edit, int column,TQPtrList<TQRegExp> & reqExpList,bool b) {
  // We don't save the old state so
  // disable the tristate mode
  chkBox->setTristate(false);
  action->setChecked(b);
  chkBox->setChecked(b);

  HiddenListViewItem* item;
  for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
       item = static_cast<HiddenListViewItem*>(item->nextSibling()))
  {
    if (!item->isSelected())
        continue;
        
    if (b == item->isOn(column))
        continue;
            
    if (!b) {
        TQRegExp* rx = getRegExpListMatch(item->text(0),reqExpList);
        
        // Perhaps the file was hidden because it started with a dot
        if (!rx && item->text(0)[0]=='.' && _dlg->hideDotFilesChk->isChecked()) {
            int result = KMessageBox::questionYesNo(_dlg,i18n(
                    "<qt>Some files you have selected are hidden because they start with a dot; "
                    "do you want to uncheck all files starting with a dot?</qt>"),i18n("Files Starting With Dot"),i18n("Uncheck Hidden"), i18n("Keep Hidden"));
                
            if (result == KMessageBox::No) {
                TQPtrList<HiddenListViewItem> lst = getMatchingItems(TQRegExp(".*",false,true));
                deselect(lst);
            } else {
                _dlg->hideDotFilesChk->setChecked(false);
            }
            continue;
        } else {
            if (rx) {
                // perhaps it is matched by a wildcard string
                TQString p = rx->pattern();
                if ( p.find("*") > -1 ||
                        p.find("?") > -1 )
                {
                    // TODO after message freeze: why show three times the wildcard string? Once should be enough.
		    // TODO remove <b></b> and use <qt> instead
                    int result = KMessageBox::questionYesNo(_dlg,i18n(
                    "<b></b>Some files you have selected are matched by the wildcarded string <b>'%1'</b>; "
                    "do you want to uncheck all files matching <b>'%1'</b>?").arg(rx->pattern()).arg(rx->pattern()).arg(rx->pattern()),
                    i18n("Wildcarded String"),i18n("Uncheck Matches"),i18n("Keep Selected"));
            
                    TQPtrList<HiddenListViewItem> lst = getMatchingItems( *rx );
            
                    if (result == KMessageBox::No) {
                        deselect(lst);
                    } else {
                        setState(lst,column,false);
                        reqExpList.remove(rx);
                        updateEdit(edit, reqExpList);
                    }
                    continue;
                } else {
                    reqExpList.remove(rx);
                    updateEdit(edit, reqExpList);
                }
            }   
        }
    }
    else {
        reqExpList.append( new TQRegExp(item->text(0)) );
        updateEdit(edit, reqExpList);
    }
    
    item->setOn(column,b);
  }

  _dlg->hiddenListView->update();
}

void HiddenFileView::hiddenChkClicked(bool b)
{
    checkBoxClicked(_dlg->hiddenChk, _hiddenActn, _dlg->hiddenEdit, COL_HIDDEN,_hiddenList,b);

}

void HiddenFileView::vetoOplockChkClicked(bool b) {
    checkBoxClicked(_dlg->vetoOplockChk, _vetoOplockActn, _dlg->vetoOplockEdit, COL_VETO_OPLOCK,_vetoOplockList,b);
}

void HiddenFileView::vetoChkClicked(bool b)
{
    checkBoxClicked(_dlg->vetoChk, _vetoActn, _dlg->vetoEdit, COL_VETO,_vetoList,b);
}

/**
 * Sets the text of the TQLineEdit edit to the entries of the passed TQRegExp-List
 **/
void HiddenFileView::updateEdit(TQLineEdit* edit, TQPtrList<TQRegExp> & lst)
{
  TQString s="";

  TQRegExp* rx;
  for(rx = static_cast<TQRegExp*>(lst.first()); rx;
      rx = static_cast<TQRegExp*>(lst.next()) )
  {
    s+= rx->pattern()+TQString("/");
  }

  edit->setText(s);
}


void HiddenFileView::setState(TQPtrList<HiddenListViewItem> & lst, int column, bool b) {
  HiddenListViewItem* item;
  for(item = static_cast<HiddenListViewItem*>(lst.first()); item;
      item = static_cast<HiddenListViewItem*>(lst.next()) )
  {
    item->setOn(column,b);
  }
}


void HiddenFileView::deselect(TQPtrList<HiddenListViewItem> & lst)
{
  HiddenListViewItem* item;
  for(item = static_cast<HiddenListViewItem*>(lst.first()); item;
      item = static_cast<HiddenListViewItem*>(lst.next()) )
  {
    item->setSelected(false);
  }
}


TQPtrList<HiddenListViewItem> HiddenFileView::getMatchingItems(const TQRegExp & rx)
{
  TQPtrList<HiddenListViewItem> lst;

  HiddenListViewItem* item;
  for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
       item = static_cast<HiddenListViewItem*>(item->nextSibling()))
  {
    if (rx.exactMatch(item->text(0)))
       lst.append(item);
  }

  return lst;
}

void HiddenFileView::updateView()
{
  _hiddenList = createRegExpList(_dlg->hiddenEdit->text());
  _vetoList = createRegExpList(_dlg->vetoEdit->text());
  _vetoOplockList = createRegExpList(_dlg->vetoOplockEdit->text());

  HiddenListViewItem* item;
  for (item = static_cast<HiddenListViewItem*>(_dlg->hiddenListView->firstChild());item;
       item = static_cast<HiddenListViewItem*>(item->nextSibling()))
  {
    item->setOn(COL_HIDDEN,matchHidden(item->text(0)));
    item->setOn(COL_VETO,matchVeto(item->text(0)));
    item->setOn(COL_VETO_OPLOCK,matchVetoOplock(item->text(0)));
  }

  _dlg->hiddenListView->repaint();
}


TQPtrList<TQRegExp> HiddenFileView::createRegExpList(const TQString & s)
{
  TQPtrList<TQRegExp> lst;
  bool cs = _share->getBoolValue("case sensitive");

  if (!s.isEmpty())
  {
    TQStringList l = TQStringList::split("/",s);

    for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it ) {
        lst.append( new TQRegExp(*it,cs,true) );
    }
  }

  return lst;
}

bool HiddenFileView::matchHidden(const TQString & s)
{
  TQPtrList<TQRegExp> hiddenList(_hiddenList);

  if (_dlg->hideDotFilesChk->isChecked())
     hiddenList.append( new TQRegExp(".*",false,true) );

  return matchRegExpList(s,hiddenList);
}

bool HiddenFileView::matchVeto(const TQString & s)
{
  return matchRegExpList(s,_vetoList);
}

bool HiddenFileView::matchVetoOplock(const TQString & s)
{
  return matchRegExpList(s,_vetoOplockList);
}

bool HiddenFileView::matchRegExpList(const TQString & s, TQPtrList<TQRegExp> & lst)
{
  if (getRegExpListMatch(s,lst))
     return true;
  else
     return false;
}


TQRegExp* HiddenFileView::getHiddenMatch(const TQString & s)
{
  return getRegExpListMatch(s,_hiddenList);
}

TQRegExp* HiddenFileView::getVetoMatch(const TQString & s)
{
  return getRegExpListMatch(s,_vetoList);
}

TQRegExp* HiddenFileView::getRegExpListMatch(const TQString & s, TQPtrList<TQRegExp> & lst)
{
  TQRegExp* rx;

  for ( rx = lst.first(); rx; rx = lst.next() )
  {
    if (rx->exactMatch(s))
       return rx;
  }

  return 0L;
}

void HiddenFileView::hideDotFilesChkClicked(bool)
{
  updateView();
}

void HiddenFileView::hideUnreadableChkClicked(bool)
{
  updateView();
}

void HiddenFileView::slotMouseButtonPressed( int, TQListViewItem*, const TQPoint&, int c ) {
  columnClicked(c);
}


#include "hiddenfileview.moc"