summaryrefslogtreecommitdiff
path: root/core/apps/textedit/fileBrowser.cpp
Unidiff
Diffstat (limited to 'core/apps/textedit/fileBrowser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/apps/textedit/fileBrowser.cpp634
1 files changed, 0 insertions, 634 deletions
diff --git a/core/apps/textedit/fileBrowser.cpp b/core/apps/textedit/fileBrowser.cpp
deleted file mode 100644
index 3798f90..0000000
--- a/core/apps/textedit/fileBrowser.cpp
+++ b/dev/null
@@ -1,634 +0,0 @@
1/****************************************************************************
2** copyright 2001 ljp ljp@llornkcor.com
3** Created: Fri Dec 14 08:16:46 2001
4**
5** This file may be distributed and/or modified under the terms of the
6** GNU General Public License version 2 as published by the Free Software
7** Foundation and appearing in the file LICENSE.GPL included in the
8** packaging of this file.
9**
10** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12**
13****************************************************************************/
14#define QTOPIA_INTERNAL_MIMEEXT
15#include "fileBrowser.h"
16//#include "inputDialog.h"
17
18#include <qpe/config.h>
19#include <qpe/resource.h>
20#include <qpe/fileselector.h>
21#include <qpe/qpeapplication.h>
22#include <qpe/menubutton.h>
23#include <qpe/mimetype.h>
24
25#include <qdict.h>
26#include <qwidgetstack.h>
27#include <qlistview.h>
28#include <qcombo.h>
29#include <qpushbutton.h>
30#include <qfile.h>
31#include <qmessagebox.h>
32#include <qlayout.h>
33#include <unistd.h>
34#include <qpopupmenu.h>
35#include <qlineedit.h>
36#include <qstringlist.h>
37
38#include <unistd.h>
39#include <stdlib.h>
40
41static int u_id = 1;
42static int get_unique_id()
43{
44 return u_id++;
45}
46
47fileBrowser::fileBrowser( QWidget* parent, const char* name, bool modal, WFlags fl , const QString mimeFilter )
48 : QDialog( parent, name, modal, fl )
49{
50 if ( !name )
51 setName( "fileBrowser" );
52 setCaption(tr( name ) );
53
54// getMimeTypes();
55
56// mimeType = mimeFilter;
57// MimeType mt( mimeType);
58
59// if( mt.extension().isEmpty()) {
60
61// QStringList filterList;
62// filterList=QStringList::split(";",mimeFilter,FALSE);
63
64// for ( QStringList::Iterator it = filterList.begin(); it != filterList.end(); ++it ) {
65// printf( "%s \n", (*it).latin1() );
66// }
67
68// filterStr = mimeFilter.right(mimeFilter.length() - mimeFilter.find("/",0,TRUE) - 1);// "*";
69
70// qDebug(filterStr);
71// } else {
72 filterStr = "*";//+ mt.extension();
73// qDebug("description "+mt.description());
74// qDebug( "id "+mt.id());
75// qDebug("extension "+mt.extension());
76// }
77// channel = new QCopChannel( "QPE/fileDialog", this );
78// connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
79// this, SLOT(receive(const QCString&, const QByteArray&)) );
80
81 QGridLayout *layout = new QGridLayout( this );
82 layout->setSpacing( 4 );
83 layout->setMargin( 4 );
84
85 dirPathCombo = new QComboBox( FALSE, this, "dirPathCombo" );
86 dirPathCombo->setEditable(TRUE);
87
88 connect( dirPathCombo, SIGNAL( activated( const QString & ) ),
89 this, SLOT( dirPathComboActivated( const QString & ) ) );
90
91 connect( dirPathCombo->lineEdit(), SIGNAL( returnPressed( ) ),
92 this, SLOT( dirPathEditPressed( ) ) );
93
94 dirPathStringList << "/";
95// we can get the storage here
96
97 layout->addMultiCellWidget( dirPathCombo, 0, 0, 0, 4 );
98
99 cdUpButton = new QPushButton(Resource::loadIconSet("up"),"",this,"cdUpButton");
100 cdUpButton ->setMinimumSize( QSize( 20, 20 ) );
101 cdUpButton ->setMaximumSize( QSize( 20, 20 ) );
102 connect( cdUpButton ,SIGNAL(released()),this,SLOT( upDir()) );
103 cdUpButton ->setFlat(TRUE);
104 layout->addMultiCellWidget( cdUpButton, 0, 0, 5, 5 );
105
106 docButton = new QPushButton(Resource::loadIconSet("DocsIcon"),"",this,"docsButton");
107 docButton->setMinimumSize( QSize( 20, 20 ) );
108 docButton->setMaximumSize( QSize( 20, 20 ) );
109 connect( docButton,SIGNAL(released()),this,SLOT( docButtonPushed()) );
110 docButton->setFlat(TRUE);
111 layout->addMultiCellWidget( docButton, 0, 0, 6, 6 );
112
113 homeButton = new QPushButton( Resource::loadIconSet("home"),"",this,"homeButton");
114 homeButton->setMinimumSize( QSize( 20, 20 ) );
115 homeButton->setMaximumSize( QSize( 20, 20 ) );
116 connect(homeButton,SIGNAL(released()),this,SLOT(homeButtonPushed()) );
117 homeButton->setFlat(TRUE);
118 layout->addMultiCellWidget( homeButton, 0, 0, 7, 7 );
119
120 FileStack = new QWidgetStack( this );
121
122 ListView = new QListView( this, "ListView" );
123// ListView->setMinimumSize( QSize( 100, 25 ) );
124 ListView->addColumn( tr( "Name" ) );
125 ListView->setColumnWidth(0,120);
126 ListView->setSorting( 2, FALSE);
127 ListView->addColumn( tr( "Size" ) );
128 ListView->setColumnWidth(1,-1);
129 ListView->addColumn( "Date",-1);
130
131 ListView->setColumnWidthMode(0,QListView::Manual);
132 ListView->setColumnAlignment(1,QListView::AlignRight);
133 ListView->setColumnAlignment(2,QListView::AlignRight);
134 ListView->setAllColumnsShowFocus( TRUE );
135
136 QPEApplication::setStylusOperation( ListView->viewport(),QPEApplication::RightOnHold);
137 connect( ListView, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
138 this, SLOT( ListPressed(int, QListViewItem *, const QPoint&, int)) );
139
140 connect( ListView, SIGNAL( clicked( QListViewItem*)), SLOT(listClicked(QListViewItem *)) );
141
142 FileStack->addWidget( ListView, get_unique_id() );
143mimeType="text/plain";
144 fileSelector = new FileSelector( mimeType, FileStack, "fileselector" , FALSE, FALSE); //buggy
145// connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( showEditTools() ) );
146// connect( fileSelector, SIGNAL( newSelected( const DocLnk &) ), this, SLOT( newFile( const DocLnk & ) ) );
147 connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ),
148 this, SLOT( docOpen( const DocLnk & ) ) );
149 layout->addMultiCellWidget( FileStack, 1, 1, 0, 7 );
150
151 SelectionCombo = new QComboBox( FALSE, this, "SelectionCombo" );
152 SelectionCombo->insertItem( tr( "Documents" ) );
153 SelectionCombo->insertItem( tr( "All files" ) );
154 SelectionCombo->insertItem( tr( "Hidden files" ) );
155// SelectionCombo->setMaximumWidth(120);
156 layout->addMultiCellWidget( SelectionCombo, 2, 2, 0, 3 );
157
158 connect( SelectionCombo, SIGNAL( activated( const QString & ) ),
159 this, SLOT( selectionChanged( const QString & ) ) );
160
161 typemb = new MenuButton(this);
162 typemb->setLabel(tr("Type: %1"));
163 typemb->setMinimumWidth(110);
164 typemb->setFixedHeight(22);
165 layout->addMultiCellWidget( typemb, 2, 2, 4, 7 );
166 updateMimeTypeMenu() ;
167
168 currentDir.setPath(QDir::currentDirPath());
169 currentDir.setFilter( QDir::Files | QDir::Dirs/* | QDir::Hidden */| QDir::All);
170 currentDir.setNameFilter(filterStr);
171
172 populateList();
173 move(0,15);
174}
175
176fileBrowser::~fileBrowser()
177{
178}
179
180void fileBrowser::setFileView( int selection )
181{
182 SelectionCombo->setCurrentItem( selection );
183 selectionChanged( SelectionCombo->currentText() );
184}
185
186void fileBrowser::populateList()
187{
188 ListView->clear();
189 QListViewItem * item;
190 bool isDir=FALSE;
191//qDebug(currentDir.canonicalPath());
192 currentDir.setSorting(/* QDir::Size*/ /*| QDir::Reversed | */QDir::DirsFirst);
193 currentDir.setMatchAllDirs(TRUE);
194
195// currentDir.setNameFilter("*.txt;*.etx");
196 QString fileL, fileS, fileDate;
197 const QFileInfoList *list = currentDir.entryInfoList( /*QDir::All*/ /*, QDir::SortByMask*/);
198 QFileInfoListIterator it(*list);
199 QFileInfo *fi;
200 while ( (fi=it.current()) ) {
201 if (fi->isSymLink() ){
202 QString symLink=fi->readLink();
203// qDebug("Symlink detected "+symLink);
204 QFileInfo sym( symLink);
205 fileS.sprintf( "%10li", sym.size() );
206 fileL.sprintf( "%s -> %s", sym.fileName().data(),sym.absFilePath().data() );
207 fileDate = sym.lastModified().toString();
208 } else {
209// qDebug("Not a dir: "+currentDir.canonicalPath()+fileL);
210 fileS.sprintf( "%10li", fi->size() );
211 fileL.sprintf( "%s",fi->fileName().data() );
212 fileDate= fi->lastModified().toString();
213 if( QDir(QDir::cleanDirPath(currentDir.canonicalPath()+"/"+fileL)).exists() ) {
214 fileL+="/";
215 isDir=TRUE;
216// qDebug( fileL);
217 }
218 }
219 if(fileL !="./" && fi->exists()) {
220 item= new QListViewItem( ListView,fileL,fileS , fileDate);
221 QPixmap pm;
222
223 if(isDir || fileL.find("/",0,TRUE) != -1) {
224 if( !QDir( fi->filePath() ).isReadable())
225 pm = Resource::loadPixmap( "lockedfolder" );
226 else
227 pm= Resource::loadPixmap( "folder" );
228 item->setPixmap( 0,pm );
229 } else {
230 if( !fi->isReadable() )
231 pm = Resource::loadPixmap( "locked" );
232 else {
233 MimeType mt(fi->filePath());
234 pm=mt.pixmap();// sets the pixmap for the mimetype
235 if(pm.isNull())
236 pm = Resource::loadPixmap( "UnknownDocument-14" );
237 item->setPixmap( 0,pm);
238 }
239 }
240 if( fileL.find("->",0,TRUE) != -1) {
241 // overlay link image
242 pm= Resource::loadPixmap( "folder" );
243 QPixmap lnk = Resource::loadPixmap( "opie/symlink" );
244 QPainter painter( &pm );
245 painter.drawPixmap( pm.width()-lnk.width(), pm.height()-lnk.height(), lnk );
246 pm.setMask( pm.createHeuristicMask( FALSE ) );
247 item->setPixmap( 0, pm);
248 }
249 }
250 isDir=FALSE;
251 ++it;
252// }
253 }
254 ListView->setSorting( 3, FALSE);
255 QString currentPath = currentDir.canonicalPath();
256
257 fillCombo( (const QString &)currentPath);
258// dirPathCombo->lineEdit()->setText(currentPath);
259
260// if( dirPathStringList.grep(currentPath,TRUE).isEmpty() ) {
261// dirPathCombo->clear();
262// dirPathStringList.prepend(currentPath );
263// dirPathCombo->insertStringList( dirPathStringList,-1);
264// }
265}
266
267void fileBrowser::upDir()
268{
269 QString current = currentDir.canonicalPath();
270 QDir dir(current);
271 dir.cdUp();
272 current = dir.canonicalPath();
273 chdir( current.latin1() );
274 currentDir.cd( current, TRUE);
275 populateList();
276 update();
277}
278
279// you may want to switch these 2 functions. I like single clicks
280void fileBrowser::listClicked(QListViewItem *selectedItem)
281{
282 if(selectedItem) {
283 QString strItem=selectedItem->text(0);
284 QString strSize=selectedItem->text(1);
285// qDebug("strItem is "+strItem);
286 strSize.stripWhiteSpace();
287// qDebug(strSize);
288
289 if(strItem.find("@",0,TRUE) !=-1 || strItem.find("->",0,TRUE) !=-1 ) { //if symlink
290 // is symlink
291 QString strItem2=strItem.right( (strItem.length()-strItem.find("->",0,TRUE)) -4);
292// qDebug("strItem symlink is "+strItem2);
293 if(QDir(strItem2).exists() ) {
294 currentDir.cd(strItem2, TRUE);
295 populateList();
296 }
297 } else { // not a symlink
298 if(strItem.find(". .",0,TRUE) && strItem.find("/",0,TRUE)!=-1 ) {
299 if(QDir(QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem)).exists() ) {
300 strItem=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem);
301 currentDir.cd(strItem,FALSE);
302// qDebug("Path is "+strItem);
303 populateList();
304 } else {
305 currentDir.cdUp();
306 populateList();
307 }
308 if(QDir(strItem).exists()){
309 currentDir.cd(strItem, TRUE);
310 populateList();
311 }
312 } else {
313 strItem=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+strItem);
314 if( QFile::exists(strItem ) ) {
315//currentDir.canonicalPath()
316 qDebug("We found our files!!"+strItem);
317 OnOK();
318 }
319 } //end not symlink
320 chdir(strItem.latin1());
321 }
322 }
323}
324
325void fileBrowser::OnOK()
326{
327 QListViewItemIterator it1( ListView);
328 for ( ; it1.current(); ++it1 ) {
329 if ( it1.current()->isSelected() ) {
330 selectedFileName=QDir::cleanDirPath(currentDir.canonicalPath()+"/"+it1.current()->text(0));
331 qDebug("selected filename is "+selectedFileName);
332 fileList.append( selectedFileName );
333 }
334 }
335 accept();
336}
337
338void fileBrowser::homeButtonPushed() {
339 QString current = QDir::homeDirPath();
340 chdir( current.latin1() );
341 currentDir.cd( current, TRUE);
342 populateList();
343 update();
344}
345
346void fileBrowser::docButtonPushed() {
347 QString current = QPEApplication::documentDir();
348 chdir( current.latin1() );
349 currentDir.cd( current, TRUE);
350 populateList();
351 update();
352
353}
354
355void fileBrowser::selectionChanged( const QString &select )
356{
357 if ( select == "Documents") {
358 FileStack->raiseWidget( fileSelector );
359 dirPathCombo->hide();
360 cdUpButton->hide();
361 docButton->hide();
362 homeButton->hide();
363 } else {
364 if ( select == "All files" )
365 currentDir.setFilter( QDir::Files | QDir::Dirs | QDir::All);
366 else
367 currentDir.setFilter( QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All);
368
369 populateList();
370 update();
371 dirPathCombo->show();
372 cdUpButton->show();
373 docButton->show();
374 homeButton->show();
375 FileStack->raiseWidget( ListView );
376 }
377}
378
379void fileBrowser::docOpen( const DocLnk &doc )
380{
381 fileList.append( doc.file().latin1() );
382 accept();
383}
384
385void fileBrowser::ListPressed( int mouse, QListViewItem *item, const QPoint &point, int i)
386{
387 switch (mouse) {
388 case 1:
389 break;
390 case 2:
391 showListMenu(item);
392 break;
393 };
394}
395
396void fileBrowser::showListMenu(QListViewItem *item) {
397
398 QPopupMenu m;// = new QPopupMenu( Local_View );
399 if(item) {
400 if( item->text(0).find("/",0,TRUE))
401 m.insertItem( tr( "Change Directory" ), this, SLOT( doCd() ));
402 m.insertItem( tr( "Make Directory" ), this, SLOT( makDir() ));
403 m.insertItem( tr( "Rescan" ), this, SLOT( populateList() ));
404 m.insertItem( tr( "Rename" ), this, SLOT( localRename() ));
405 m.insertSeparator();
406 m.insertItem( tr( "Delete" ), this, SLOT( localDelete() ));
407 } else {
408 m.insertItem( tr( "Make Directory" ), this, SLOT( makDir() ));
409 m.insertItem( tr( "Rescan" ), this, SLOT( populateList() ));
410
411 }
412 m.exec( QCursor::pos() );
413}
414
415void fileBrowser::doCd() {
416 listClicked( ListView->currentItem());
417}
418
419void fileBrowser::makDir() {
420 InputDialog *fileDlg;
421 fileDlg = new InputDialog(this,"Make Directory",TRUE, 0);
422 fileDlg->exec();
423 if( fileDlg->result() == 1 ) {
424 QString filename = fileDlg->LineEdit1->text();
425 qDebug("Make dir");
426 currentDir.mkdir( currentDir.canonicalPath()+"/"+filename);
427 }
428 populateList();
429}
430
431void fileBrowser::localRename() {
432 QString curFile = ListView->currentItem()->text(0);
433 InputDialog *fileDlg;
434 fileDlg = new InputDialog(this,"Rename",TRUE, 0);
435 fileDlg->setTextEdit((const QString &) curFile);
436 fileDlg->exec();
437 if( fileDlg->result() == 1 ) {
438 QString oldname = currentDir.canonicalPath() + "/" + curFile;
439 QString newName = currentDir.canonicalPath() + "/" + fileDlg->LineEdit1->text();//+".playlist";
440 if( rename(oldname.latin1(), newName.latin1())== -1)
441 QMessageBox::message("Note","Could not rename");
442 }
443 populateList();
444}
445
446void fileBrowser::localDelete() {
447 QString f = ListView->currentItem()->text(0);
448 if(QDir(f).exists() ) {
449 switch ( QMessageBox::warning(this,"Delete","Do you really want to delete\n"+f+
450 " ?\nIt must be empty","Yes","No",0,0,1) ) {
451 case 0: {
452 f=currentDir.canonicalPath()+"/"+f;
453 QString cmd="rmdir "+f;
454 system( cmd.latin1());
455 populateList();
456 }
457 break;
458 case 1:
459 // exit
460 break;
461 };
462 } else {
463 switch ( QMessageBox::warning(this,"Delete","Do you really want to delete\n"+f
464 +" ?","Yes","No",0,0,1) ) {
465 case 0: {
466 f=currentDir.canonicalPath()+"/"+f;
467 QString cmd="rm "+f;
468 system( cmd.latin1());
469 populateList();
470 }
471 break;
472 case 1:
473 // exit
474 break;
475 };
476 }
477}
478
479void fileBrowser::updateMimeTypeMenu() {
480
481 disconnect( typemb, SIGNAL(selected(const QString&)),
482 this, SLOT(showType(const QString&)) );
483
484 QString prev;
485
486 // Type filter
487 QStringList types;
488 types << tr("All");
489 types << "--";
490 types += getMimeTypes();
491 prev = typemb->currentText();
492 typemb->clear();
493 typemb->insertItems(types);
494 // typemb->select(prev);
495
496 connect(typemb, SIGNAL(selected(const QString&)), this, SLOT(showType(const QString&)));
497}
498
499void fileBrowser::showType(const QString &t) {
500
501// qDebug("Show type "+t);
502 if(t.find("All",0,TRUE) != -1) {
503 filterStr = "*";
504 } else {
505 QStringList list = mimetypes.grep( t,TRUE);
506 QString ext;
507 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
508 mimeType =(*it);
509 MimeType mt(mimeType);
510
511// qDebug("mime "+mimeType);
512// qDebug("description "+mt.description());
513// qDebug( "id "+mt.id());
514// qDebug("extension "+mt.extension());
515
516 if( mt.extension().isEmpty())
517 filterStr = "*";
518 else
519 filterStr = "*."+ mt.extension()+" ";
520// printf( "%s \n", (*it).latin1() );
521 }
522 }
523 currentDir.setNameFilter(filterStr);
524
525 populateList();
526 update();
527// if(fileSelector) {
528// disconnect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( docOpen( const DocLnk & ) ) );
529// delete fileSelector;
530 // }
531 // fileSelector = new FileSelector( mimeType, FileStack, "fileselector" , FALSE, FALSE); //buggy
532// connect( fileSelector, SIGNAL( closeMe() ), this, SLOT( showEditTools() ) );
533// connect( fileSelector, SIGNAL( newSelected( const DocLnk &) ), this, SLOT( newFile( const DocLnk & ) ) );
534 // connect( fileSelector, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( docOpen( const DocLnk & ) ) );
535// fileSelector->reread();
536// if ( t == tr("All") ) {
537// icons->setTypeFilter("",TRUE);
538// } else {
539// icons->setTypeFilter(t+"/*",TRUE);
540// }
541
542}
543
544QStringList fileBrowser::getMimeTypes() {
545
546 QStringList r;
547 AppLnkSet apps( QPEApplication::qpeDir() + "apps" );
548 QFile file( QPEApplication::qpeDir()+"etc/available.mime");
549 file.open( IO_WriteOnly|IO_Truncate);//)
550 for ( QListIterator<AppLnk> it( apps.children() ); it.current(); ++it ) {
551 AppLnk* l;
552 l = it.current();
553 QStringList maj = l->mimeTypes();
554 QStringList::ConstIterator f;
555 for ( f = maj.begin(); f != maj.end(); f++ ) {
556 QString temp = *f;
557 qDebug("type "+temp);
558 mimetypes << temp;
559 int sl = temp.find('/');
560 if (sl >= 0) {
561 QString k = temp;//.left(sl);
562 if( r.grep(k,TRUE).isEmpty() ) {
563 r << k;
564 k+="\n";
565 file.writeBlock( k.latin1(), k.length());
566 }
567 }
568 }
569 }
570 r.sort();
571 file.close();
572 return r;
573}
574
575void fileBrowser::receive( const QCString &msg, const QByteArray &data ) {
576// QDataStream stream( data, IO_ReadOnly );
577// if (msg == "keyRegister(int key, QString channel, QString message)")
578// {
579// int k;
580// QString c, m;
581// stream >> k;
582// stream >> c;
583// stream >> m;
584}
585
586void fileBrowser::dirPathComboActivated( const QString & current) {
587 chdir( current.latin1() );
588 currentDir.cd( current, TRUE);
589 populateList();
590 update();
591}
592
593void fileBrowser::dirPathEditPressed() {
594 QString current = dirPathCombo->lineEdit()->text();
595 chdir( current.latin1() );
596 currentDir.cd( current, TRUE);
597 populateList();
598 update();
599}
600
601void fileBrowser::fillCombo(const QString &currentPath) {
602
603 dirPathCombo->lineEdit()->setText(currentPath);
604
605 if( dirPathStringList.grep(currentPath,TRUE).isEmpty() ) {
606 dirPathCombo->clear();
607 dirPathStringList.prepend(currentPath );
608 dirPathCombo->insertStringList( dirPathStringList,-1);
609 }
610}
611
612
613InputDialog::InputDialog( QWidget* parent, const char* name, bool modal, WFlags fl )
614 : QDialog( parent, name, modal, fl )
615{
616 if ( !name )
617 setName( "InputDialog" );
618 resize( 234, 50 );
619 setMaximumSize( QSize( 240, 50 ) );
620 setCaption( tr(name ) );
621
622 LineEdit1 = new QLineEdit( this, "LineEdit1" );
623 LineEdit1->setGeometry( QRect( 10, 10, 216, 22 ) );
624}
625
626InputDialog::~InputDialog()
627{
628 inputText= LineEdit1->text();
629
630}
631
632void InputDialog::setTextEdit(const QString &string) {
633 LineEdit1->setText(string);
634}