summaryrefslogtreecommitdiff
path: root/core/apps/textedit/fileBrowser.cpp
blob: 7eac5d2f245f1b812905e330fc48264ee5d3d2f4 (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
/****************************************************************************
** copyright 2001 ljp ljp@llornkcor.com
** Created: Fri Dec 14 08:16:46 2001
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "fileBrowser.h"
#include "inputDialog.h"

#include <qpe/config.h>
#include <qpe/resource.h>
#include <qpe/fileselector.h>
#include <qpe/qpeapplication.h>
#include <qpe/menubutton.h>

#include <qdict.h>
#include <qwidgetstack.h>
#include <qlistview.h>
#include <qcombo.h>
#include <qpushbutton.h>
#include <qfile.h>
#include <qmessagebox.h>
#include <qlayout.h>
#include <unistd.h>
#include <qpopupmenu.h>
#include <qlineedit.h>
#include <qstringlist.h>

#include <unistd.h>
#include <stdlib.h>

static int u_id = 1;
static int get_unique_id()
{
    return u_id++;
}

fileBrowser::fileBrowser( QWidget* parent,  const char* name, bool modal, WFlags fl , const QString filter )
    : QDialog( parent, name, modal, fl )
{
    if ( !name )
        setName( "fileBrowser" );
    setCaption(tr( name ) );
    filterStr=filter;

//      channel = new QCopChannel( "QPE/fileDialog", this );
//      connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
//         this, SLOT(receive(const QCString&, const QByteArray&)) );

    QGridLayout *layout = new QGridLayout( this );
    layout->setSpacing( 4 );
    layout->setMargin( 4 );

    dirPathCombo = new QComboBox( FALSE, this, "dorPathCombo" );
    dirPathCombo->setEditable(TRUE);

    connect( dirPathCombo, SIGNAL( activated( const QString & ) ),
              this, SLOT( dirPathComboActivated( const QString & ) ) );

    connect( dirPathCombo->lineEdit(), SIGNAL( returnPressed(  ) ),
              this, SLOT( dirPathEditPressed( ) ) );

    dirPathStringList << "/";
// we can get the storage here

    layout->addMultiCellWidget( dirPathCombo, 0, 0, 0, 4 );

    cdUpButton = new QPushButton(Resource::loadIconSet("up"),"",this,"cdUpButton");
    cdUpButton ->setMinimumSize( QSize( 20, 20 ) );
    cdUpButton ->setMaximumSize( QSize( 20, 20 ) );
    connect( cdUpButton ,SIGNAL(released()),this,SLOT( upDir()) );
    cdUpButton ->setFlat(TRUE);
    layout->addMultiCellWidget( cdUpButton, 0, 0, 5, 5 );


    docButton = new QPushButton(Resource::loadIconSet("DocsIcon"),"",this,"docsButton");
    docButton->setMinimumSize( QSize( 20, 20 ) );
    docButton->setMaximumSize( QSize( 20, 20 ) );
    connect( docButton,SIGNAL(released()),this,SLOT( docButtonPushed()) );
    docButton->setFlat(TRUE);
    layout->addMultiCellWidget( docButton, 0, 0, 6, 6 );

    homeButton = new QPushButton( Resource::loadIconSet("home"),"",this,"homeButton");
    homeButton->setMinimumSize( QSize( 20, 20 ) );
    homeButton->setMaximumSize( QSize( 20, 20 ) );
    connect(homeButton,SIGNAL(released()),this,SLOT(homeButtonPushed()) );
    homeButton->setFlat(TRUE);
    layout->addMultiCellWidget( homeButton, 0, 0, 7, 7 );

    FileStack = new QWidgetStack( this );


    ListView = new QListView( this, "ListView" );
    ListView->setMinimumSize( QSize( 100, 25 ) );
    ListView->addColumn( tr( "Name" ) );
    ListView->setColumnWidth(0,120);
    ListView->setSorting( 2, FALSE);
    ListView->addColumn( tr( "Size" ) );
    ListView->setColumnWidth(1,-1);
    ListView->addColumn( "Date",-1);

    ListView->setColumnWidthMode(0,QListView::Manual);
    ListView->setColumnAlignment(1,QListView::AlignRight);
    ListView->setColumnAlignment(2,QListView::AlignRight);
    ListView->setAllColumnsShowFocus( TRUE );

    QPEApplication::setStylusOperation( ListView->viewport(),QPEApplication::RightOnHold);
    connect( ListView, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
             this,SLOT( ListPressed(int, QListViewItem *, const QPoint&, int)) );

    connect( ListView, SIGNAL( clicked( QListViewItem*)), SLOT(listClicked(QListViewItem *)) );

    FileStack->addWidget( ListView, get_unique_id() );
    mimeType="text/*";
    fileSelector = new FileSelector( mimeType, FileStack, "fileselector" , FALSE, FALSE); //buggy
//    connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( showEditTools() ) );
//    connect( fileSelector, SIGNAL( newSelected( const DocLnk &) ), this, SLOT( newFile( const DocLnk & ) ) );
    connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( docOpen( const DocLnk & ) ) );
    layout->addMultiCellWidget( FileStack, 1, 1, 0, 7 );

    SelectionCombo = new QComboBox( FALSE, this, "SelectionCombo" );
    SelectionCombo->insertItem( tr( "Documents" ) );
    SelectionCombo->insertItem( tr( "All files" ) );
    SelectionCombo->insertItem( tr( "Hidden files" ) );
//      SelectionCombo->setMaximumWidth(120);
    layout->addMultiCellWidget( SelectionCombo, 2, 2, 0, 3 );

    connect( SelectionCombo, SIGNAL( activated( const QString & ) ),
            this, SLOT( selectionChanged( const QString & ) ) );

    typemb = new MenuButton(this);
    typemb->setLabel(tr("Type: %1"));
    typemb->setMinimumWidth(110);
    typemb->setFixedHeight(22);
    layout->addMultiCellWidget( typemb, 2, 2, 4, 7 );
    updateMimeTypeMenu() ;

    currentDir.setPath(QDir::currentDirPath());
    currentDir.setFilter( QDir::Files | QDir::Dirs/* | QDir::Hidden */| QDir::All);

    populateList();
    move(0,15);
}

fileBrowser::~fileBrowser()
{
}

void fileBrowser::setFileView( int selection )
{
    SelectionCombo->setCurrentItem( selection );
    selectionChanged( SelectionCombo->currentText() );
}

void fileBrowser::populateList()
{
    ListView->clear();
    bool isDir=FALSE;
//qDebug(currentDir.canonicalPath());
    currentDir.setSorting(/* QDir::Size*/ /*| QDir::Reversed | */QDir::DirsFirst);
    currentDir.setMatchAllDirs(TRUE);

    currentDir.setNameFilter(filterStr);
//    currentDir.setNameFilter("*.txt;*.etx");
    QString fileL, fileS, fileDate;
    const QFileInfoList *list = currentDir.entryInfoList( /*QDir::All*/ /*, QDir::SortByMask*/);
    QFileInfoListIterator it(*list);
    QFileInfo *fi;
    while ( (fi=it.current()) ) {

        if (fi->isSymLink() ){
            QString symLink=fi->readLink();
//         qDebug("Symlink detected "+symLink);
         QFileInfo sym( symLink);
            fileS.sprintf( "%10li", sym.size() );
            fileL.sprintf( "%s ->  %s",  sym.fileName().data(),sym.absFilePath().data() );
            fileDate = sym.lastModified().toString();
        } else {
//        qDebug("Not a dir: "+currentDir.canonicalPath()+fileL);
            fileS.sprintf( "%10li", fi->size() );
            fileL.sprintf( "%s",fi->fileName().data() );
            fileDate= fi->lastModified().toString();
            if( QDir(QDir::cleanDirPath(currentDir.canonicalPath()+"/"+fileL)).exists() ) {
                fileL+="/";
                isDir=TRUE;
//     qDebug( fileL);
            }
        }
        if(fileL !="./") {
        item= new QListViewItem( ListView,fileL,fileS , fileDate);
        if(isDir || fileL.find("/",0,TRUE) != -1)
            item->setPixmap( 0,  Resource::loadPixmap( "folder" ));
        else
            item->setPixmap( 0, Resource::loadPixmap( "fileopen" ));
        }
        isDir=FALSE;
        ++it;
    }
    ListView->setSorting( 3, FALSE);
    QString currentPath = currentDir.canonicalPath();

    fillCombo( (const QString &)currentPath);
//   dirPathCombo->lineEdit()->setText(currentPath);

//      if( dirPathStringList.grep(currentPath,TRUE).isEmpty() ) {
//          dirPathCombo->clear();
//          dirPathStringList.prepend(currentPath );
//          dirPathCombo->insertStringList( dirPathStringList,-1);
//      }
}

void fileBrowser::upDir()
{
    QString current = currentDir.canonicalPath();
    QDir dir(current);
    dir.cdUp();
    current = dir.canonicalPath();
    chdir( current.latin1() );
    currentDir.cd(  current, TRUE);
    populateList();
    update();
}

// you may want to switch these 2 functions. I like single clicks
void fileBrowser::listClicked(QListViewItem *selectedItem)
{
    QString strItem=selectedItem->text(0);
    QString strSize=selectedItem->text(1);
//    qDebug("strItem is "+strItem);
    strSize.stripWhiteSpace();
//  qDebug(strSize);

        if(strItem.find("@",0,TRUE) !=-1 || strItem.find("->",0,TRUE) !=-1 ) { //if symlink
              // is symlink
            QString strItem2=strItem.right( (strItem.length()-strItem.find("->",0,TRUE)) -4);
//            qDebug("strItem symlink is "+strItem2);
            if(QDir(strItem2).exists() ) {
                currentDir.cd(strItem2, TRUE);
                populateList();
            }
        } else { // not a symlink
            if(strItem.find(". .",0,TRUE) && strItem.find("/",0,TRUE)!=-1 ) {
                if(QDir(QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem)).exists() ) {
                    strItem=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem);
                    currentDir.cd(strItem,FALSE);
//       qDebug("Path is "+strItem);
                    populateList();
                } else {
                    currentDir.cdUp();
                    populateList();
                }
                if(QDir(strItem).exists()){
                    currentDir.cd(strItem, TRUE);
                    populateList();
                }
            } else {
                strItem=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem);
                  if( QFile::exists(strItem ) ) {
//currentDir.canonicalPath()
                      qDebug("We found our files!!"+strItem);
                    OnOK();
                  }
                } //end not symlink
        chdir(strItem.latin1());
      }
}

void fileBrowser::OnOK()
{
  QListViewItemIterator it1( ListView);
    for ( ; it1.current(); ++it1 ) {
      if ( it1.current()->isSelected() ) {
         selectedFileName=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+it1.current()->text(0));
         qDebug("selected filename is "+selectedFileName);
       fileList.append( selectedFileName );
      }
    }
  accept();
}

void fileBrowser::homeButtonPushed() {
    QString current = QDir::homeDirPath();
    chdir( current.latin1() );
    currentDir.cd(  current, TRUE);
    populateList();
    update();
}

void fileBrowser::docButtonPushed() {
    QString current = QPEApplication::documentDir();
    chdir( current.latin1() );
    currentDir.cd( current, TRUE);
    populateList();
    update();

}

void fileBrowser::selectionChanged( const QString &select )
{
    if ( select == "Documents")  {
        FileStack->raiseWidget( fileSelector );
        dirPathCombo->hide();
        cdUpButton->hide();
        docButton->hide();
        homeButton->hide();
    } else {
        if ( select == "All files" )
            currentDir.setFilter( QDir::Files | QDir::Dirs | QDir::All);
        else
            currentDir.setFilter( QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All);

        populateList();
        update();
        dirPathCombo->show();
        cdUpButton->show();
        docButton->show();
        homeButton->show();
        FileStack->raiseWidget( ListView );
    }
}

void fileBrowser::docOpen( const DocLnk &doc )
{
    fileList.append( doc.file().latin1() );
    accept();
}

void fileBrowser::ListPressed( int mouse, QListViewItem *item, const QPoint &point, int i)
{
    switch (mouse) {
      case 1:
          break;
      case 2:
          showListMenu(item);
    break;
    };
}

void fileBrowser::showListMenu(QListViewItem *item) {

    QPopupMenu  m;// = new QPopupMenu( Local_View );
    if( item->text(0).find("/",0,TRUE))
    m.insertItem( tr( "Change Directory" ), this, SLOT( doCd() ));
    else
    m.insertItem( tr( "Make Directory" ), this, SLOT( makDir() ));
    m.insertItem( tr( "Rescan" ), this, SLOT( populateList()() ));
    m.insertItem( tr( "Rename" ), this, SLOT( localRename() ));
    m.insertSeparator();
    m.insertItem( tr( "Delete" ), this, SLOT( localDelete() ));
    m.exec( QCursor::pos() );

}

void fileBrowser::doCd() {
    listClicked( ListView->currentItem());
}

void fileBrowser::makDir() {
    InputDialog *fileDlg;
    fileDlg = new InputDialog(this,"Make Directory",TRUE, 0);
    fileDlg->exec();
    if( fileDlg->result() == 1 ) {
       QString  filename = fileDlg->LineEdit1->text();
       currentDir.mkdir( currentDir.canonicalPath()+"/"+filename);
    }
    populateList();
}

void fileBrowser::localRename() {
    QString curFile = ListView->currentItem()->text(0);
    InputDialog *fileDlg;
    fileDlg = new InputDialog(this,"Rename",TRUE, 0);
    fileDlg->inputText = curFile;
    fileDlg->exec();
    if( fileDlg->result() == 1 ) {
        QString oldname =  currentDir.canonicalPath() + "/" + curFile;
        QString newName =  currentDir.canonicalPath() + "/" + fileDlg->LineEdit1->text();//+".playlist";
        if( rename(oldname.latin1(), newName.latin1())== -1)
            QMessageBox::message("Note","Could not rename");
    }
    populateList();
}

void fileBrowser::localDelete() {
    QString f = ListView->currentItem()->text(0);
    if(QDir(f).exists() ) {
        switch ( QMessageBox::warning(this,"Delete","Do you really want to delete\n"+f+
                                      " ?\nIt must be empty","Yes","No",0,0,1) ) {
          case 0: {
              f=currentDir.canonicalPath()+"/"+f;
              QString cmd="rmdir "+f;
              system( cmd.latin1());
              populateList();
          }
              break;
          case 1:
                // exit
              break;
        };

    } else {
        switch ( QMessageBox::warning(this,"Delete","Do you really want to delete\n"+f
                                      +" ?","Yes","No",0,0,1) ) {
          case 0: {
              f=currentDir.canonicalPath()+"/"+f;
              QString cmd="rm "+f;
              system( cmd.latin1());
              populateList();
          }
              break;
          case 1:
                // exit
              break;
        };
    }
}

void fileBrowser::updateMimeTypeMenu() {

    disconnect( typemb, SIGNAL(selected(const QString&)),
          this, SLOT(showType(const QString&)) );

    QString prev;

    // Type filter
    QStringList types;
    types << tr("All");
    types << "--";
    types += getMimeTypes();
    prev = typemb->currentText();
    typemb->clear();
    typemb->insertItems(types);
      //  typemb->select(prev);

    connect(typemb, SIGNAL(selected(const QString&)), this, SLOT(showType(const QString&)));
}

void fileBrowser::showType(const QString &t) {

    qDebug(t);
    mimeType = t+"/*";
//      if(fileSelector) {
//      disconnect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( docOpen( const DocLnk & ) ) );
//          delete fileSelector;
      //  }
      //   fileSelector = new FileSelector( mimeType, FileStack, "fileselector" , FALSE, FALSE); //buggy
//    connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( showEditTools() ) );
//    connect( fileSelector, SIGNAL( newSelected( const DocLnk &) ), this, SLOT( newFile( const DocLnk & ) ) );
      //   connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( docOpen( const DocLnk & ) ) );
//    fileSelector->reread();
    repaint();
//      if ( t == tr("All") ) {
//          icons->setTypeFilter("",TRUE);
//      } else {
//          icons->setTypeFilter(t+"/*",TRUE);
//      }

}

QStringList fileBrowser::getMimeTypes() {
    QStringList r;
    AppLnkSet apps( QPEApplication::qpeDir() + "apps" );
    QFile file( QPEApplication::qpeDir()+"etc/available.mime");
    file.open( IO_WriteOnly|IO_Truncate);//)
    for ( QListIterator<AppLnk> it( apps.children() ); it.current(); ++it ) {
        AppLnk* l;
        l = it.current();
        QStringList maj = l->mimeTypes();
        QStringList::ConstIterator f;
        for (  f = maj.begin(); f != maj.end(); f++ ) {
            QString  temp = *f;
            int sl = temp.find('/');
            if (sl >= 0) {
                QString k = temp.left(sl);
                if( r.grep(k,TRUE).isEmpty() ) {
                    r << k;
                    k+="\n";
                    file.writeBlock(  k.latin1(), k.length());
                }
            }
        }
    }
    r.sort();
    file.close();
    return r;
}

void fileBrowser::receive( const QCString &msg, const QByteArray &data ) {
//    QDataStream stream( data, IO_ReadOnly );
//    if (msg == "keyRegister(int key, QString channel, QString message)")
//    {
//      int k;
//      QString c, m;
//      stream >> k;
//      stream >> c;
//      stream >> m;
}

void fileBrowser::dirPathComboActivated( const QString & current) {
    chdir( current.latin1() );
    currentDir.cd( current, TRUE);
    populateList();
    update();
}

void fileBrowser::dirPathEditPressed() {
    QString current = dirPathCombo->lineEdit()->text();
    chdir( current.latin1() );
    currentDir.cd( current, TRUE);
    populateList();
    update();
}

void fileBrowser::fillCombo(const QString &currentPath) {

    dirPathCombo->lineEdit()->setText(currentPath);

    if( dirPathStringList.grep(currentPath,TRUE).isEmpty() ) {
        dirPathCombo->clear();
        dirPathStringList.prepend(currentPath );
        dirPathCombo->insertStringList( dirPathStringList,-1);
    }

}