author | eilers <eilers> | 2003-08-08 15:09:29 (UTC) |
---|---|---|
committer | eilers <eilers> | 2003-08-08 15:09:29 (UTC) |
commit | b87b33dc1f6e8e1276a7035ab7a3e53b8ed4bd16 (patch) (unidiff) | |
tree | b0f96db67cfb1b9d2476530ecec9a2ba6eacbab1 /libopie/ofileselector.cpp | |
parent | 14d394e6c107b037a09a31a92605034fe50f7813 (diff) | |
download | opie-b87b33dc1f6e8e1276a7035ab7a3e53b8ed4bd16.zip opie-b87b33dc1f6e8e1276a7035ab7a3e53b8ed4bd16.tar.gz opie-b87b33dc1f6e8e1276a7035ab7a3e53b8ed4bd16.tar.bz2 |
Merging all changes from BRANCH_1_0.
-rw-r--r-- | libopie/ofileselector.cpp | 856 |
1 files changed, 856 insertions, 0 deletions
diff --git a/libopie/ofileselector.cpp b/libopie/ofileselector.cpp new file mode 100644 index 0000000..577e916 --- a/dev/null +++ b/libopie/ofileselector.cpp | |||
@@ -0,0 +1,856 @@ | |||
1 | #include <qcombobox.h> | ||
2 | #include <qdir.h> | ||
3 | #include <qhbox.h> | ||
4 | #include <qheader.h> | ||
5 | #include <qlabel.h> | ||
6 | #include <qlayout.h> | ||
7 | #include <qlineedit.h> | ||
8 | #include <qlistview.h> | ||
9 | #include <qpopupmenu.h> | ||
10 | #include <qwidgetstack.h> | ||
11 | #include <qregexp.h> | ||
12 | #include <qobjectlist.h> | ||
13 | |||
14 | /* hacky but we need to get FileSelector::filter */ | ||
15 | #define private public | ||
16 | #include <qpe/fileselector.h> | ||
17 | #undef private | ||
18 | |||
19 | #include <qpe/qpeapplication.h> | ||
20 | #include <qpe/mimetype.h> | ||
21 | #include <qpe/resource.h> | ||
22 | #include <qpe/storage.h> | ||
23 | |||
24 | #include "ofileselector_p.h" | ||
25 | #include "ofileselector.h" | ||
26 | |||
27 | |||
28 | |||
29 | OFileViewInterface::OFileViewInterface( OFileSelector* selector ) | ||
30 | : m_selector( selector ) { | ||
31 | } | ||
32 | OFileViewInterface::~OFileViewInterface() { | ||
33 | } | ||
34 | QString OFileViewInterface::name()const{ | ||
35 | return m_name; | ||
36 | } | ||
37 | void OFileViewInterface::setName( const QString& name ) { | ||
38 | m_name = name; | ||
39 | } | ||
40 | OFileSelector* OFileViewInterface::selector()const { | ||
41 | return m_selector; | ||
42 | } | ||
43 | DocLnk OFileViewInterface::selectedDocument()const { | ||
44 | return DocLnk( selectedName() ); | ||
45 | } | ||
46 | bool OFileViewInterface::showNew()const { | ||
47 | return selector()->showNew(); | ||
48 | } | ||
49 | bool OFileViewInterface::showClose()const { | ||
50 | return selector()->showClose(); | ||
51 | } | ||
52 | MimeTypes OFileViewInterface::mimeTypes()const { | ||
53 | return selector()->mimeTypes(); | ||
54 | } | ||
55 | QStringList OFileViewInterface::currentMimeType()const { | ||
56 | return selector()->currentMimeType(); | ||
57 | } | ||
58 | void OFileViewInterface::activate( const QString& ) { | ||
59 | // not implemented here | ||
60 | } | ||
61 | void OFileViewInterface::ok() { | ||
62 | emit selector()->ok(); | ||
63 | } | ||
64 | void OFileViewInterface::cancel() { | ||
65 | emit selector()->cancel(); | ||
66 | } | ||
67 | void OFileViewInterface::closeMe() { | ||
68 | emit selector()->closeMe(); | ||
69 | } | ||
70 | void OFileViewInterface::fileSelected( const QString& str) { | ||
71 | emit selector()->fileSelected( str); | ||
72 | } | ||
73 | void OFileViewInterface::fileSelected( const DocLnk& lnk) { | ||
74 | emit selector()->fileSelected( lnk ); | ||
75 | } | ||
76 | void OFileViewInterface::setCurrentFileName( const QString& str ) { | ||
77 | selector()->m_lneEdit->setText( str ); | ||
78 | } | ||
79 | QString OFileViewInterface::currentFileName()const{ | ||
80 | return selector()->m_lneEdit->text(); | ||
81 | } | ||
82 | QString OFileViewInterface::startDirectory()const{ | ||
83 | return selector()->m_startDir; | ||
84 | } | ||
85 | |||
86 | |||
87 | ODocumentFileView::ODocumentFileView( OFileSelector* selector ) | ||
88 | : OFileViewInterface( selector ) { | ||
89 | m_selector = 0; | ||
90 | setName( QObject::tr("Documents") ); | ||
91 | } | ||
92 | ODocumentFileView::~ODocumentFileView() { | ||
93 | |||
94 | } | ||
95 | QString ODocumentFileView::selectedName()const { | ||
96 | if (!m_selector) | ||
97 | return QString::null; | ||
98 | |||
99 | return m_selector->selectedDocument().file(); | ||
100 | } | ||
101 | QString ODocumentFileView::selectedPath()const { | ||
102 | return QPEApplication::documentDir(); | ||
103 | } | ||
104 | QString ODocumentFileView::directory()const { | ||
105 | return selectedPath(); | ||
106 | } | ||
107 | void ODocumentFileView::reread() { | ||
108 | if (!m_selector) | ||
109 | return; | ||
110 | |||
111 | m_selector->setNewVisible( showNew() ); | ||
112 | m_selector->setCloseVisible( showClose() ); | ||
113 | m_selector->filter = currentMimeType().join(";"); | ||
114 | m_selector->reread(); | ||
115 | } | ||
116 | int ODocumentFileView::fileCount()const { | ||
117 | if (!m_selector) | ||
118 | return -1; | ||
119 | |||
120 | return m_selector->fileCount(); | ||
121 | } | ||
122 | DocLnk ODocumentFileView::selectedDocument()const { | ||
123 | if (!m_selector) | ||
124 | return DocLnk(); | ||
125 | |||
126 | return m_selector->selectedDocument(); | ||
127 | } | ||
128 | QWidget* ODocumentFileView::widget( QWidget* parent ) { | ||
129 | if (!m_selector ) { | ||
130 | m_selector = new FileSelector(currentMimeType().join(";"), parent, "fileselector", showNew(), showClose() ); | ||
131 | QObject::connect(m_selector, SIGNAL(fileSelected( const DocLnk& ) ), | ||
132 | selector(), SLOT(slotDocLnkBridge(const DocLnk&) ) ); | ||
133 | QObject::connect(m_selector, SIGNAL(closeMe() ), | ||
134 | selector(), SIGNAL(closeMe() ) ); | ||
135 | QObject::connect(m_selector, SIGNAL(newSelected(const DocLnk& ) ), | ||
136 | selector(), SIGNAL(newSelected(const DocLnk& ) ) ); | ||
137 | } | ||
138 | |||
139 | return m_selector; | ||
140 | } | ||
141 | |||
142 | /* | ||
143 | * This is the file system view used | ||
144 | * we use a QListView + QListViewItems for it | ||
145 | */ | ||
146 | |||
147 | OFileSelectorItem::OFileSelectorItem( QListView* view, const QPixmap& pixmap, | ||
148 | const QString& path, const QString& date, | ||
149 | const QString& size, const QString& dir, | ||
150 | bool isLocked, bool isDir ) | ||
151 | : QListViewItem( view ) | ||
152 | { | ||
153 | setPixmap(0, pixmap ); | ||
154 | setText(1, path ); | ||
155 | setText(2, size ); | ||
156 | setText(3, date ); | ||
157 | m_isDir = isDir; | ||
158 | m_dir = dir; | ||
159 | m_locked = isLocked; | ||
160 | } | ||
161 | OFileSelectorItem::~OFileSelectorItem() { | ||
162 | |||
163 | } | ||
164 | bool OFileSelectorItem::isLocked()const { | ||
165 | return m_locked; | ||
166 | } | ||
167 | QString OFileSelectorItem::directory()const { | ||
168 | return m_dir; | ||
169 | } | ||
170 | bool OFileSelectorItem::isDir()const { | ||
171 | return m_isDir; | ||
172 | } | ||
173 | QString OFileSelectorItem::path()const { | ||
174 | return text( 1 ); | ||
175 | } | ||
176 | QString OFileSelectorItem::key( int id, bool )const { | ||
177 | QString ke; | ||
178 | if( id == 0 || id == 1 ){ // name | ||
179 | if( m_isDir ){ | ||
180 | ke.append("0" ); | ||
181 | ke.append( text(1) ); | ||
182 | }else{ | ||
183 | ke.append("1" ); | ||
184 | ke.append( text(1) ); | ||
185 | } | ||
186 | return ke; | ||
187 | }else | ||
188 | return text( id ); | ||
189 | |||
190 | } | ||
191 | |||
192 | OFileViewFileListView::OFileViewFileListView( QWidget* parent, const QString& startDir, | ||
193 | OFileSelector* sel) | ||
194 | : QWidget( parent ), m_sel( sel ) { | ||
195 | m_all = false; | ||
196 | QVBoxLayout* lay = new QVBoxLayout( this ); | ||
197 | m_currentDir = startDir; | ||
198 | |||
199 | /* | ||
200 | * now we add a special bar | ||
201 | * One Button For Up | ||
202 | * Home | ||
203 | * Doc | ||
204 | * And a dropdown menu with FileSystems | ||
205 | * FUTURE: one to change dir with lineedit | ||
206 | * Bookmarks | ||
207 | * Create Dir | ||
208 | */ | ||
209 | QHBox* box = new QHBox(this ); | ||
210 | box->setBackgroundMode( PaletteButton ); | ||
211 | box->setSpacing( 0 ); | ||
212 | |||
213 | QToolButton *btn = new QToolButton( box ); | ||
214 | btn->setIconSet( Resource::loadIconSet("up") ); | ||
215 | connect(btn, SIGNAL(clicked() ), | ||
216 | this, SLOT( cdUP() ) ); | ||
217 | |||
218 | btn = new QToolButton( box ); | ||
219 | btn->setIconSet( Resource::loadIconSet("home") ); | ||
220 | connect(btn, SIGNAL(clicked() ), | ||
221 | this, SLOT( cdHome() ) ); | ||
222 | |||
223 | btn = new QToolButton( box ); | ||
224 | btn->setIconSet( Resource::loadIconSet("DocsIcon") ); | ||
225 | connect(btn, SIGNAL(clicked() ), | ||
226 | this, SLOT(cdDoc() ) ); | ||
227 | |||
228 | m_btnNew = new QToolButton( box ); | ||
229 | m_btnNew->setIconSet( Resource::loadIconSet("new") ); | ||
230 | connect(m_btnNew, SIGNAL(clicked() ), | ||
231 | this, SLOT(slotNew() ) ); | ||
232 | |||
233 | |||
234 | m_btnClose = new QToolButton( box ); | ||
235 | m_btnClose->setIconSet( Resource::loadIconSet("close") ); | ||
236 | connect(m_btnClose, SIGNAL(clicked() ), | ||
237 | selector(), SIGNAL(closeMe() ) ); | ||
238 | |||
239 | btn = new QToolButton( box ); | ||
240 | btn->setIconSet( Resource::loadIconSet("cardmon/pcmcia") ); | ||
241 | |||
242 | /* let's fill device parts */ | ||
243 | QPopupMenu* pop = new QPopupMenu(this); | ||
244 | connect(pop, SIGNAL( activated(int) ), | ||
245 | this, SLOT(slotFSActivated(int) ) ); | ||
246 | |||
247 | StorageInfo storage; | ||
248 | const QList<FileSystem> &fs = storage.fileSystems(); | ||
249 | QListIterator<FileSystem> it(fs); | ||
250 | for ( ; it.current(); ++it ) { | ||
251 | const QString disk = (*it)->name(); | ||
252 | const QString path = (*it)->path(); | ||
253 | m_dev.insert( disk, path ); | ||
254 | pop->insertItem( disk ); | ||
255 | } | ||
256 | m_fsPop = pop; | ||
257 | |||
258 | |||
259 | btn->setPopup( pop ); | ||
260 | |||
261 | lay->addWidget( box ); | ||
262 | |||
263 | m_view = new QListView( this ); | ||
264 | |||
265 | m_view->installEventFilter(this); | ||
266 | |||
267 | QPEApplication::setStylusOperation( m_view->viewport(), | ||
268 | QPEApplication::RightOnHold); | ||
269 | m_view->addColumn(" " ); | ||
270 | m_view->addColumn(tr("Name"), 135 ); | ||
271 | m_view->addColumn(tr("Size"), -1 ); | ||
272 | m_view->addColumn(tr("Date"), 60 ); | ||
273 | m_view->addColumn(tr("Mime Type"), -1 ); | ||
274 | |||
275 | |||
276 | m_view->setSorting( 1 ); | ||
277 | m_view->setAllColumnsShowFocus( TRUE ); | ||
278 | |||
279 | lay->addWidget( m_view, 1000 ); | ||
280 | connectSlots(); | ||
281 | } | ||
282 | OFileViewFileListView::~OFileViewFileListView() { | ||
283 | } | ||
284 | void OFileViewFileListView::slotNew() { | ||
285 | DocLnk lnk; | ||
286 | emit selector()->newSelected( lnk ); | ||
287 | } | ||
288 | OFileSelectorItem* OFileViewFileListView::currentItem()const{ | ||
289 | QListViewItem* item = m_view->currentItem(); | ||
290 | if (!item ) | ||
291 | return 0l; | ||
292 | |||
293 | return static_cast<OFileSelectorItem*>(item); | ||
294 | } | ||
295 | void OFileViewFileListView::reread( bool all ) { | ||
296 | m_view->clear(); | ||
297 | |||
298 | if (selector()->showClose() ) | ||
299 | m_btnClose->show(); | ||
300 | else | ||
301 | m_btnClose->hide(); | ||
302 | |||
303 | if (selector()->showNew() ) | ||
304 | m_btnNew->show(); | ||
305 | else | ||
306 | m_btnNew->hide(); | ||
307 | |||
308 | m_mimes = selector()->currentMimeType(); | ||
309 | m_all = all; | ||
310 | |||
311 | QDir dir( m_currentDir ); | ||
312 | if (!dir.exists() ) | ||
313 | return; | ||
314 | |||
315 | dir.setSorting( QDir::Name | QDir::DirsFirst | QDir::Reversed ); | ||
316 | int filter; | ||
317 | if (m_all ) | ||
318 | filter = QDir::Files | QDir::Dirs | QDir::Hidden | QDir::All; | ||
319 | else | ||
320 | filter = QDir::Files | QDir::Dirs | QDir::All; | ||
321 | dir.setFilter( filter ); | ||
322 | |||
323 | // now go through all files | ||
324 | const QFileInfoList *list = dir.entryInfoList(); | ||
325 | if (!list) { | ||
326 | cdUP(); | ||
327 | return; | ||
328 | } | ||
329 | QFileInfoListIterator it( *list ); | ||
330 | QFileInfo *fi; | ||
331 | while( (fi=it.current() ) ){ | ||
332 | if( fi->fileName() == QString::fromLatin1("..") || fi->fileName() == QString::fromLatin1(".") ){ | ||
333 | ++it; | ||
334 | continue; | ||
335 | } | ||
336 | |||
337 | /* | ||
338 | * It is a symlink we try to resolve it now but don't let us attack by DOS | ||
339 | * | ||
340 | */ | ||
341 | if( fi->isSymLink() ){ | ||
342 | QString file = fi->dirPath( true ) + "/" + fi->readLink(); | ||
343 | for( int i = 0; i<=4; i++) { // 5 tries to prevent dos | ||
344 | QFileInfo info( file ); | ||
345 | if( !info.exists() ){ | ||
346 | addSymlink( fi, TRUE ); | ||
347 | break; | ||
348 | }else if( info.isDir() ){ | ||
349 | addDir( fi, TRUE ); | ||
350 | break; | ||
351 | }else if( info.isFile() ){ | ||
352 | addFile( fi, TRUE ); | ||
353 | break; | ||
354 | }else if( info.isSymLink() ){ | ||
355 | file = info.dirPath(true ) + "/" + info.readLink() ; | ||
356 | break; | ||
357 | }else if( i == 4){ // couldn't resolve symlink add it as symlink | ||
358 | addSymlink( fi ); | ||
359 | } | ||
360 | } // off for loop for symlink resolving | ||
361 | }else if( fi->isDir() ) | ||
362 | addDir( fi ); | ||
363 | else if( fi->isFile() ) | ||
364 | addFile( fi ); | ||
365 | |||
366 | ++it; | ||
367 | } // of while loop | ||
368 | m_view->sort(); | ||
369 | |||
370 | } | ||
371 | int OFileViewFileListView::fileCount()const{ | ||
372 | return m_view->childCount(); | ||
373 | } | ||
374 | QString OFileViewFileListView::currentDir()const{ | ||
375 | return m_currentDir; | ||
376 | } | ||
377 | OFileSelector* OFileViewFileListView::selector() { | ||
378 | return m_sel; | ||
379 | } | ||
380 | |||
381 | bool OFileViewFileListView::eventFilter (QObject *o, QEvent *e) { | ||
382 | if ( e->type() == QEvent::KeyPress ) { | ||
383 | QKeyEvent *k = (QKeyEvent *)e; | ||
384 | if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) { | ||
385 | slotClicked( Qt::LeftButton,m_view->currentItem(),QPoint(0,0),0); | ||
386 | return true; | ||
387 | } | ||
388 | } | ||
389 | return false; | ||
390 | } | ||
391 | |||
392 | |||
393 | void OFileViewFileListView::connectSlots() { | ||
394 | connect(m_view, SIGNAL(clicked(QListViewItem*) ), | ||
395 | this, SLOT(slotCurrentChanged(QListViewItem*) ) ); | ||
396 | connect(m_view, SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint&, int ) ), | ||
397 | this, SLOT(slotClicked(int, QListViewItem*, const QPoint&, int ) ) ); | ||
398 | } | ||
399 | void OFileViewFileListView::slotCurrentChanged( QListViewItem* item) { | ||
400 | if (!item) | ||
401 | return; | ||
402 | #if 0 | ||
403 | |||
404 | OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item); | ||
405 | |||
406 | if (!sel->isDir() ) { | ||
407 | selector()->m_lneEdit->setText( sel->text(1) ); | ||
408 | // if in fileselector mode we will emit selected | ||
409 | if ( selector()->mode() == OFileSelector::FileSelector ) { | ||
410 | qWarning("slot Current Changed"); | ||
411 | QStringList str = QStringList::split("->", sel->text(1) ); | ||
412 | QString path = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
413 | emit selector()->fileSelected( path ); | ||
414 | DocLnk lnk( path ); | ||
415 | emit selector()->fileSelected( lnk ); | ||
416 | } | ||
417 | } | ||
418 | #endif | ||
419 | } | ||
420 | void OFileViewFileListView::slotClicked(int button , QListViewItem* item, const QPoint&, int ) { | ||
421 | if (!item || ( button != Qt::LeftButton) ) | ||
422 | return; | ||
423 | |||
424 | OFileSelectorItem *sel = static_cast<OFileSelectorItem*>(item); | ||
425 | if (!sel->isLocked() ) { | ||
426 | QStringList str = QStringList::split("->", sel->text(1) ); | ||
427 | if (sel->isDir() ) { | ||
428 | m_currentDir = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
429 | emit selector()->dirSelected( m_currentDir ); | ||
430 | reread( m_all ); | ||
431 | }else { // file | ||
432 | qWarning("slot Clicked"); | ||
433 | selector()->m_lneEdit->setText( str[0].stripWhiteSpace() ); | ||
434 | QString path = sel->directory() + "/" + str[0].stripWhiteSpace(); | ||
435 | emit selector()->fileSelected( path ); | ||
436 | DocLnk lnk( path ); | ||
437 | emit selector()->fileSelected( lnk ); | ||
438 | } | ||
439 | } // not locked | ||
440 | } | ||
441 | void OFileViewFileListView::addFile( QFileInfo* info, bool symlink ) { | ||
442 | MimeType type( info->absFilePath() ); | ||
443 | if (!compliesMime( type.id() ) ) | ||
444 | return; | ||
445 | |||
446 | QPixmap pix = type.pixmap(); | ||
447 | QString dir, name; bool locked; | ||
448 | if ( pix.isNull() ) { | ||
449 | QWMatrix matrix; | ||
450 | QPixmap pixer(Resource::loadPixmap("UnknownDocument") ); | ||
451 | matrix.scale( .4, .4 ); | ||
452 | pix = pixer.xForm( matrix ); | ||
453 | } | ||
454 | dir = info->dirPath( true ); | ||
455 | locked = false; | ||
456 | if ( symlink ) | ||
457 | name = info->fileName() + " -> " + info->dirPath() + "/" + info->readLink(); | ||
458 | else{ | ||
459 | name = info->fileName(); | ||
460 | if ( ( (selector()->mode() == OFileSelector::Open)&& !info->isReadable() ) || | ||
461 | ( (selector()->mode() == OFileSelector::Save)&& !info->isWritable() ) ) { | ||
462 | locked = true; pix = Resource::loadPixmap("locked"); | ||
463 | } | ||
464 | } | ||
465 | (void)new OFileSelectorItem( m_view, pix, name, | ||
466 | info->lastModified().toString(), QString::number( info->size() ), | ||
467 | dir, locked ); | ||
468 | } | ||
469 | void OFileViewFileListView::addDir( QFileInfo* info, bool symlink ) { | ||
470 | bool locked = false; QString name; QPixmap pix; | ||
471 | |||
472 | if ( ( ( selector()->mode() == OFileSelector::Open ) && !info->isReadable() ) || | ||
473 | ( ( selector()->mode() == OFileSelector::Save ) && !info->isWritable() ) ) { | ||
474 | locked = true; | ||
475 | if ( symlink ) | ||
476 | pix = Resource::loadPixmap( "opie/symlink" ); | ||
477 | else | ||
478 | pix = Resource::loadPixmap( "lockedfolder" ); | ||
479 | }else | ||
480 | pix = symlink ? Resource::loadPixmap( "opie/symlink") : Resource::loadPixmap("folder"); | ||
481 | |||
482 | name = symlink ? info->fileName() + " -> " + info->dirPath(true) + "/" + info->readLink() : | ||
483 | info->fileName(); | ||
484 | |||
485 | (void)new OFileSelectorItem( m_view, pix, name, | ||
486 | info->lastModified().toString(), | ||
487 | QString::number( info->size() ), | ||
488 | info->dirPath( true ), locked, true ); | ||
489 | |||
490 | |||
491 | } | ||
492 | void OFileViewFileListView::addSymlink( QFileInfo* , bool ) { | ||
493 | |||
494 | } | ||
495 | void OFileViewFileListView::cdUP() { | ||
496 | QDir dir( m_currentDir ); | ||
497 | dir.cdUp(); | ||
498 | |||
499 | if (!dir.exists() ) | ||
500 | m_currentDir = "/"; | ||
501 | else | ||
502 | m_currentDir = dir.absPath(); | ||
503 | |||
504 | emit selector()->dirSelected( m_currentDir ); | ||
505 | reread( m_all ); | ||
506 | } | ||
507 | void OFileViewFileListView::cdHome() { | ||
508 | m_currentDir = QDir::homeDirPath(); | ||
509 | emit selector()->dirSelected( m_currentDir ); | ||
510 | reread( m_all ); | ||
511 | } | ||
512 | void OFileViewFileListView::cdDoc() { | ||
513 | m_currentDir = QPEApplication::documentDir(); | ||
514 | emit selector()->dirSelected( m_currentDir ); | ||
515 | reread( m_all ); | ||
516 | } | ||
517 | void OFileViewFileListView::changeDir( const QString& dir ) { | ||
518 | m_currentDir = dir; | ||
519 | emit selector()->dirSelected( m_currentDir ); | ||
520 | reread( m_all ); | ||
521 | } | ||
522 | void OFileViewFileListView::slotFSActivated( int id ) { | ||
523 | changeDir ( m_dev[m_fsPop->text(id)] ); | ||
524 | } | ||
525 | |||
526 | /* check if the mimetype in mime | ||
527 | * complies with the one which is current | ||
528 | */ | ||
529 | /* | ||
530 | * We've the mimetype of the file | ||
531 | * We need to get the stringlist of the current mimetype | ||
532 | * | ||
533 | * mime = image@slashjpeg | ||
534 | * QStringList = 'image@slash*' | ||
535 | * or QStringList = image/jpeg;image/png;application/x-ogg | ||
536 | * or QStringList = application/x-ogg;image@slash*; | ||
537 | * with all these mime filters it should get acceptes | ||
538 | * to do so we need to look if mime is contained inside | ||
539 | * the stringlist | ||
540 | * if it's contained return true | ||
541 | * if not ( I'm no RegExp expert at all ) we'll look if a '@slash*' | ||
542 | * is contained in the mimefilter and then we will | ||
543 | * look if both are equal until the '/' | ||
544 | */ | ||
545 | bool OFileViewFileListView::compliesMime( const QString& str) { | ||
546 | if (str.isEmpty() || m_mimes.isEmpty() || str.stripWhiteSpace().isEmpty() ) | ||
547 | return true; | ||
548 | |||
549 | for (QStringList::Iterator it = m_mimes.begin(); it != m_mimes.end(); ++it ) { | ||
550 | QRegExp reg( (*it) ); | ||
551 | reg.setWildcard( true ); | ||
552 | if ( str.find( reg ) != -1 ) | ||
553 | return true; | ||
554 | |||
555 | } | ||
556 | return false; | ||
557 | } | ||
558 | /* | ||
559 | * The listView giving access to the file system! | ||
560 | */ | ||
561 | class OFileViewFileSystem : public OFileViewInterface { | ||
562 | public: | ||
563 | OFileViewFileSystem( OFileSelector* ); | ||
564 | ~OFileViewFileSystem(); | ||
565 | |||
566 | QString selectedName() const; | ||
567 | QString selectedPath() const; | ||
568 | |||
569 | QString directory()const; | ||
570 | void reread(); | ||
571 | int fileCount()const; | ||
572 | |||
573 | QWidget* widget( QWidget* parent ); | ||
574 | void activate( const QString& ); | ||
575 | private: | ||
576 | OFileViewFileListView* m_view; | ||
577 | bool m_all : 1; | ||
578 | }; | ||
579 | OFileViewFileSystem::OFileViewFileSystem( OFileSelector* sel) | ||
580 | : OFileViewInterface( sel ) { | ||
581 | m_view = 0; | ||
582 | m_all = false; | ||
583 | } | ||
584 | OFileViewFileSystem::~OFileViewFileSystem() { | ||
585 | } | ||
586 | QString OFileViewFileSystem::selectedName()const{ | ||
587 | if (!m_view ) | ||
588 | return QString::null; | ||
589 | |||
590 | QString cFN=currentFileName(); | ||
591 | if (cFN.startsWith("/")) return cFN; | ||
592 | return m_view->currentDir() + "/" + cFN; | ||
593 | } | ||
594 | QString OFileViewFileSystem::selectedPath()const{ | ||
595 | return QString::null; | ||
596 | } | ||
597 | QString OFileViewFileSystem::directory()const{ | ||
598 | if (!m_view) | ||
599 | return QString::null; | ||
600 | |||
601 | OFileSelectorItem* item = m_view->currentItem(); | ||
602 | if (!item ) | ||
603 | return QString::null; | ||
604 | |||
605 | return QDir(item->directory() ).absPath(); | ||
606 | } | ||
607 | void OFileViewFileSystem::reread() { | ||
608 | if (!m_view) | ||
609 | return; | ||
610 | |||
611 | m_view->reread( m_all ); | ||
612 | } | ||
613 | int OFileViewFileSystem::fileCount()const{ | ||
614 | if (!m_view ) | ||
615 | return -1; | ||
616 | return m_view->fileCount(); | ||
617 | } | ||
618 | QWidget* OFileViewFileSystem::widget( QWidget* parent ) { | ||
619 | if (!m_view ) { | ||
620 | m_view = new OFileViewFileListView( parent, startDirectory(), selector() ); | ||
621 | } | ||
622 | return m_view; | ||
623 | } | ||
624 | void OFileViewFileSystem::activate( const QString& str) { | ||
625 | m_all = (str != QObject::tr("Files") ); | ||
626 | |||
627 | |||
628 | } | ||
629 | |||
630 | /* Selector */ | ||
631 | OFileSelector::OFileSelector( QWidget* parent, int mode, int sel, | ||
632 | const QString& dirName, const QString& fileName, | ||
633 | const MimeTypes& mimetypes, | ||
634 | bool showNew, bool showClose) | ||
635 | : QWidget( parent, "OFileSelector" ) | ||
636 | { | ||
637 | m_current = 0; | ||
638 | m_shNew = showNew; | ||
639 | m_shClose = showClose; | ||
640 | m_mimeType = mimetypes; | ||
641 | m_startDir = dirName; | ||
642 | |||
643 | m_mode = mode; | ||
644 | m_selector = sel; | ||
645 | |||
646 | initUI(); | ||
647 | m_lneEdit->setText( fileName ); | ||
648 | initMime(); | ||
649 | initViews(); | ||
650 | |||
651 | QString str; | ||
652 | switch ( m_selector ) { | ||
653 | default: | ||
654 | case Normal: | ||
655 | str = QObject::tr("Documents"); | ||
656 | m_cmbView->setCurrentItem( 0 ); | ||
657 | break; | ||
658 | case Extended: | ||
659 | str = QObject::tr("Files"); | ||
660 | m_cmbView->setCurrentItem( 1 ); | ||
661 | break; | ||
662 | case ExtendedAll: | ||
663 | str = QObject::tr("All Files"); | ||
664 | m_cmbView->setCurrentItem( 2 ); | ||
665 | break; | ||
666 | } | ||
667 | slotViewChange( str ); | ||
668 | |||
669 | } | ||
670 | OFileSelector::OFileSelector( const QString& mimeFilter, QWidget* parent, const char* name, | ||
671 | bool showNew, bool showClose ) | ||
672 | : QWidget( parent, name ) | ||
673 | { | ||
674 | m_current = 0; | ||
675 | m_shNew = showNew; | ||
676 | m_shClose = showClose; | ||
677 | m_startDir = QPEApplication::documentDir(); | ||
678 | |||
679 | if (!mimeFilter.isEmpty() ) | ||
680 | m_mimeType.insert(mimeFilter, QStringList::split(";", mimeFilter ) ); | ||
681 | |||
682 | m_mode = OFileSelector::FileSelector; | ||
683 | m_selector = OFileSelector::Normal; | ||
684 | |||
685 | initUI(); | ||
686 | initMime(); | ||
687 | initViews(); | ||
688 | m_cmbView->setCurrentItem( 0 ); | ||
689 | slotViewChange( QObject::tr("Documents") ); | ||
690 | } | ||
691 | /* | ||
692 | * INIT UI will set up the basic GUI | ||
693 | * Layout: Simple VBoxLayout | ||
694 | * On top a WidgetStack containing the Views... | ||
695 | * - List View | ||
696 | * - Document View | ||
697 | * Below we will have a Label + LineEdit | ||
698 | * Below we will have two ComoBoxes one for choosing the view one for | ||
699 | * choosing the mimetype | ||
700 | */ | ||
701 | void OFileSelector::initUI() { | ||
702 | QVBoxLayout* lay = new QVBoxLayout( this ); | ||
703 | |||
704 | m_stack = new QWidgetStack( this ); | ||
705 | lay->addWidget( m_stack, 1000 ); | ||
706 | |||
707 | m_nameBox = new QHBox( this ); | ||
708 | (void)new QLabel( tr("Name:"), m_nameBox ); | ||
709 | m_lneEdit = new QLineEdit( m_nameBox ); | ||
710 | m_lneEdit ->installEventFilter(this); | ||
711 | lay->addWidget( m_nameBox ); | ||
712 | |||
713 | m_cmbBox = new QHBox( this ); | ||
714 | m_cmbView = new QComboBox( m_cmbBox ); | ||
715 | m_cmbMime = new QComboBox( m_cmbBox ); | ||
716 | lay->addWidget( m_cmbBox ); | ||
717 | } | ||
718 | |||
719 | /* | ||
720 | * This will make sure that the return key in the name edit causes dialogs to close | ||
721 | */ | ||
722 | |||
723 | bool OFileSelector::eventFilter (QObject *o, QEvent *e) { | ||
724 | if ( e->type() == QEvent::KeyPress ) { | ||
725 | QKeyEvent *k = (QKeyEvent *)e; | ||
726 | if ( (k->key()==Key_Enter) || (k->key()==Key_Return)) { | ||
727 | emit ok(); | ||
728 | return true; | ||
729 | } | ||
730 | } | ||
731 | return false; | ||
732 | } | ||
733 | |||
734 | /* | ||
735 | * This will insert the MimeTypes into the Combo Box | ||
736 | * And also connect the changed signal | ||
737 | * | ||
738 | * AutoMimeTyping is disabled for now. It used to reparse a dir and then set available mimetypes | ||
739 | */ | ||
740 | void OFileSelector::initMime() { | ||
741 | MimeTypes::Iterator it; | ||
742 | for ( it = m_mimeType.begin(); it != m_mimeType.end(); ++it ) { | ||
743 | m_cmbMime->insertItem( it.key() ); | ||
744 | } | ||
745 | m_cmbMime->setCurrentItem( 0 ); | ||
746 | |||
747 | connect( m_cmbMime, SIGNAL(activated(int) ), | ||
748 | this, SLOT(slotMimeTypeChanged() ) ); | ||
749 | |||
750 | } | ||
751 | void OFileSelector::initViews() { | ||
752 | m_cmbView->insertItem( QObject::tr("Documents") ); | ||
753 | m_cmbView->insertItem( QObject::tr("Files") ); | ||
754 | m_cmbView->insertItem( QObject::tr("All Files") ); | ||
755 | connect(m_cmbView, SIGNAL(activated( const QString& ) ), | ||
756 | this, SLOT(slotViewChange( const QString& ) ) ); | ||
757 | |||
758 | |||
759 | m_views.insert( QObject::tr("Documents"), new ODocumentFileView(this) ); | ||
760 | |||
761 | /* see above why add both */ | ||
762 | OFileViewInterface* in = new OFileViewFileSystem( this ); | ||
763 | m_views.insert( QObject::tr("Files"), in ); | ||
764 | m_views.insert( QObject::tr("All Files"), in ); | ||
765 | } | ||
766 | OFileSelector::~OFileSelector() { | ||
767 | |||
768 | } | ||
769 | const DocLnk* OFileSelector::selected() { | ||
770 | DocLnk* lnk = new DocLnk( currentView()->selectedDocument() ); | ||
771 | return lnk; | ||
772 | } | ||
773 | QString OFileSelector::selectedName()const{ | ||
774 | return currentView()->selectedName(); | ||
775 | } | ||
776 | QString OFileSelector::selectedPath()const { | ||
777 | return currentView()->selectedPath(); | ||
778 | } | ||
779 | QString OFileSelector::directory()const { | ||
780 | return currentView()->directory(); | ||
781 | } | ||
782 | DocLnk OFileSelector::selectedDocument()const { | ||
783 | return currentView()->selectedDocument(); | ||
784 | } | ||
785 | int OFileSelector::fileCount()const { | ||
786 | return currentView()->fileCount(); | ||
787 | } | ||
788 | void OFileSelector::reread() { | ||
789 | return currentView()->reread(); | ||
790 | } | ||
791 | OFileViewInterface* OFileSelector::currentView()const{ | ||
792 | return m_current; | ||
793 | } | ||
794 | bool OFileSelector::showNew()const { | ||
795 | return m_shNew; | ||
796 | } | ||
797 | bool OFileSelector::showClose()const { | ||
798 | return m_shClose; | ||
799 | } | ||
800 | MimeTypes OFileSelector::mimeTypes()const { | ||
801 | return m_mimeType; | ||
802 | } | ||
803 | int OFileSelector::mode()const{ | ||
804 | return m_mode; | ||
805 | } | ||
806 | int OFileSelector::selector()const{ | ||
807 | return m_selector; | ||
808 | } | ||
809 | QStringList OFileSelector::currentMimeType()const { | ||
810 | return m_mimeType[m_cmbMime->currentText()]; | ||
811 | } | ||
812 | void OFileSelector::slotMimeTypeChanged() { | ||
813 | reread(); | ||
814 | } | ||
815 | void OFileSelector::slotDocLnkBridge( const DocLnk& lnk) { | ||
816 | m_lneEdit->setText( lnk.name() ); | ||
817 | emit fileSelected( lnk ); | ||
818 | emit fileSelected( lnk.name() ); | ||
819 | } | ||
820 | void OFileSelector::slotFileBridge( const QString& str) { | ||
821 | DocLnk lnk( str ); | ||
822 | emit fileSelected( lnk ); | ||
823 | } | ||
824 | void OFileSelector::slotViewChange( const QString& view ) { | ||
825 | OFileViewInterface* interface = m_views[view]; | ||
826 | if (!interface) | ||
827 | return; | ||
828 | |||
829 | interface->activate( view ); | ||
830 | if (m_current) | ||
831 | m_stack->removeWidget( m_current->widget( m_stack ) ); | ||
832 | |||
833 | static int id = 1; | ||
834 | |||
835 | m_stack->addWidget( interface->widget(m_stack), id ); | ||
836 | m_stack->raiseWidget( id ); | ||
837 | |||
838 | interface->reread(); | ||
839 | m_current = interface; | ||
840 | |||
841 | id++; | ||
842 | } | ||
843 | void OFileSelector::setNewVisible( bool b ) { | ||
844 | m_shNew = b; | ||
845 | currentView()->reread(); | ||
846 | } | ||
847 | void OFileSelector::setCloseVisible( bool b ) { | ||
848 | m_shClose = b; | ||
849 | currentView()->reread(); | ||
850 | } | ||
851 | void OFileSelector::setNameVisible( bool b ) { | ||
852 | if ( b ) | ||
853 | m_nameBox->show(); | ||
854 | else | ||
855 | m_nameBox->hide(); | ||
856 | } | ||