-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileBrowser.cpp
397 lines (345 loc) · 10.3 KB
/
FileBrowser.cpp
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
#include <AM_LauncherClient.h>
#include <qdir.h>
#include <qfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "myDef.h"
#include "FileBrowser.h"
#include "source.h"
//#ifdef ZN5
//#define ZListBoxItem ZSettingItem
//#endif
FileBrowser::FileBrowser(const QString &dir, const QString& type, QWidget* parent, WFlags f)
: ZListBox(type, parent, 0)
{
//nameFilter = QString("QDir::Dirs | QDir::Files| QDir::Hidden | QDir::System");
//dirSort = QString("QDir::DirsFirst | QDir::Name | QDir::IgnoreCase");
m_strCurDir = dir;
m_eFilterType = SHOW_ALL;
m_eMode = NORMAL;
fileCount = 0;
dirCount = 0;
isItemInfo = false;
m_bMimeTypeState = false;
m_strAppDir = getAppDir();
//#ifdef ZN5
QFont font ( qApp->font() );
font.setPointSize ( 16 );
this->setItemFont( ZListBox::LISTITEM_REGION_A, ZListBox::StStandard, font );
font.setPointSize ( 18 );
this->setItemFont( ZListBox::LISTITEM_REGION_A, ZListBox::StHighlighted, font );
this->setItemFont( ZListBox::LISTITEM_REGION_A, ZListBox::StSelected, font );
this->setItemFont( ZListBox::LISTITEM_REGION_A, ZListBox::StHighlightSelected, font );
//#endif
connect ( this, SIGNAL ( highlighted ( int ) ), this, SLOT ( itemHighlighted ( int ) ) );
connect ( this, SIGNAL ( selected ( int ) ), this, SLOT ( itemSelected ( int ) ) );
}
FileBrowser::~FileBrowser()
{
#ifdef DEBUG
qDebug("===== delete file browser \n");
#endif
}
void FileBrowser::init()
{
//mMode = NORMAL;
setDir(m_strCurDir, m_eMode);//, nameFilter);//, dirSort);
//setCurrentItem(0);
}
void FileBrowser::setDir(QString &pdir, ITEMMODE mode)//, const QString &sFilter, const QString &sSort)
{
//clear();
#ifdef DEBUGFM
std::cout<<"cur dir is "<<pdir<<std::endl;
#endif
fileCount = 0;
dirCount = 0;
m_strCurDir = pdir;
m_eMode = mode;
this->clear();
QDir dir(pdir);
#ifndef ZN5
dir.setMatchAllDirs (true);
#endif
if ( !dir.isReadable() ) {
return;
}
int index = 0;
QString itemType = QString("%I%M");
SettingItemRegion eRegion = SETTINGITEM_REGION_A;
if( mode == MULTI ) {
index ++;
itemType = QString("%C%I%M");
eRegion = SETTINGITEM_REGION_B;
}
QStringList entries;
QStringList::ConstIterator p;
ZSettingItem* pItem = NULL;
QPixmap icon;
if(m_eFilterType == SHOW_ALL) {
entries = dir.entryList( QDir::Dirs | QDir::Hidden );
} else {
entries = dir.entryList( QDir::Dirs );
}
entries.sort();
for (p=entries.begin(); p!=entries.end(); p++)
{
if ( *p == "." || *p == ".." ) continue;
icon = getItemIcon( adjustFile(QString(*p)) );
pItem = new ZSettingItem(this, itemType);
pItem->appendSubItem(index+1, *p, false, eRegion);
pItem->setPixmap(index, icon);
this->insertItem(pItem);
dirCount++;
}
if(m_eFilterType == SHOW_ALL) {
entries = dir.entryList( QDir::Files | QDir::Hidden );//| QDir::System);
} else {
entries = dir.entryList( QDir::Files );//| QDir::System);
}
entries.sort();
for (p=entries.begin(); p!=entries.end(); p++)
{
if ( *p == "." || *p == ".." ) continue;
icon = getItemIcon( adjustFile(QString(*p)) );
pItem = new ZSettingItem(this, itemType);
pItem->appendSubItem(index+1, *p, false, eRegion);
pItem->setPixmap(index, icon);
this->insertItem(pItem);
fileCount++;
}
#ifdef SCREEN_V
pItem = new ZSettingItem(this, QString("%I%M100%M"));
#else
pItem = new ZSettingItem(this, QString("%I%M140%M"));
#endif
pItem->appendSubItem(1, pdir);
pItem->appendSubItem(2, QString( " %1 Dirs %2 Files").arg(dirCount).arg(fileCount) );
this->insertItem(pItem, 0);//, false);
this->setCurrentItem(0);
}
void FileBrowser::itemHighlighted(int index)
{
if( index == 0 ) {
m_strFilePath = m_strCurDir;
m_strFileName = m_strCurDir;
isItemInfo = true;
return;
}
if( m_eMode == MULTI) {
return;
}
ZSettingItem *pItem = (ZSettingItem *)item(index);
m_strFileName = pItem->getSubItemText(1, 0);//, false);
m_strFilePath = adjustFile(m_strFileName);
isItemInfo = false;
emit fileHighlighted(index);
}
void FileBrowser::itemSelected(int index)
{
if( index == 0 ) {
isItemInfo = true;
if( m_eMode != MULTI) {
cdBack();
}
return;
}
isItemInfo = false;
QString sName = QString::null;
QString sPath = QString::null;
ZSettingItem *pItem =(ZSettingItem *)item(index);
sName = pItem->getSubItemText(1, 0);
sPath = adjustFile(sName);
#ifdef DEBUGFM
std::cout<<"path is "<<sPath<<std::endl;
#endif
if( m_eMode == NORMAL ) {
if( QFileInfo(sPath).isDir() ) {
m_strCurDir = sPath;
setDir(m_strCurDir);
} else {
if( QFileInfo(sPath).isFile() )
{
#ifdef INSTALL_MGX_IN_FM
if( sPath.endsWith( QString(".mgx") ) )
{
QString path = getAppDir();
system(QString("exec '%1/mgxInstaller' -d '%2' &").arg(path).arg(sPath));
}
else
#endif
AM_LauncherClient::openDocument(sPath);
}
}
}
}
void FileBrowser::setMode(ITEMMODE mode)
{
m_eMode = mode;
}
QStringList FileBrowser::getCheckedItemList()
{
QStringList list;// = "";
for (unsigned int i=0 ; i<this->count(); i++) {
if ( this->itemChecked(i) ) {
list += ( (ZSettingItem*)item(i) )->getSubItemText(2, 0);
}
}
return list;
}
void FileBrowser::cdBack()
{
QString newDir;
int ret = m_strCurDir.findRev('/');
if (ret <= 0) newDir = "/";
else newDir = m_strCurDir.left(ret);
m_strCurDir = newDir;
#ifdef DEBUGFM
std::cout<<"cur dir is "<<newDir<<std::endl;
#endif
setDir(m_strCurDir, NORMAL);
}
void FileBrowser::setCursor(const QString &keyItem)
{
if(m_eMode == MULTI) return;
for (unsigned int i=0 ; i<this->count(); i++) {
if( ( ((ZSettingItem*)item(i))->getSubItemText(1, 0) ).find(keyItem) > -1 ) {
this->setCurrentItem(i);
break;
}
}
}
FileBrowser::FILE_TYPE FileBrowser::getFileType( const QString &fullPath )
{
if( QFileInfo(fullPath).isDir() ) {
return FILE_DIR ;
}
if( QFileInfo(fullPath).isSymLink() ) {
return FILE_SYMLINK ;
}
QString filePath = fullPath;
filePath = filePath.remove( 0, filePath.findRev("/")+1 );
filePath = filePath.remove( 0, filePath.findRev(".")+1 );
filePath = ( QString(".%1").arg(filePath) ).lower();
if ( filePath == ".7z" || filePath == ".zip" || filePath == ".tar" ||
filePath == ".gz" || filePath == ".bz2" || filePath == ".rar" ||
filePath == ".cab" || filePath == ".iso" || filePath == ".arg" ||
filePath == ".z" )
return FILE_PACKAGE;
if ( filePath == ".lin" || filePath == ".sh" )
return FILE_SCRIPT;
if ( filePath == ".mgs" )
return FILE_THEME;
if ( filePath == ".ttf" || filePath == ".ttc" )
return FILE_FONT;
if ( filePath == ".km" )
return FILE_KM;
if ( filePath == ".qkt" )
return FILE_QKT;
if ( filePath == ".bmp" || filePath == ".png" || filePath == ".jpg" || filePath == ".jpeg" || filePath == ".gif")
return FILE_IMG;
if ( filePath == ".flv" || filePath == ".avi" || filePath == ".flac" || filePath == ".vob"|| filePath == ".rm" ||
filePath == ".rmvb" || filePath == ".3gp")
return FILE_MEDIA;
if ( filePath == ".txt" || filePath == ".cfg" || filePath == ".chm" ||
filePath == ".ini" || filePath == ".conf" || filePath == ".c" ||
filePath == ".cc" || filePath == ".cpp" || filePath == ".h" ||
filePath == ".hh" || filePath == ".java" || filePath == ".lng" ||
filePath == ".desktop" || filePath == ".log" || filePath == ".pro")
return FILE_TEXT;
return FILE_OTHER;
}
QPixmap FileBrowser::getItemIcon( const QString &fullPath )
{
if( !m_bMimeTypeState ) {
return m_iconReader.getIcon( (QFileInfo(fullPath).isFile()) ? "fm_insert_file_small.bmp" : "fm_folder_small.bmp" );
}
QString iconFile;
FILE_TYPE type = getFileType( fullPath );
switch(type) {
case FILE_PACKAGE :
iconFile = m_strAppDir+"/img/img_pkg.png";
break;
case FILE_FONT :
iconFile = m_strAppDir+"/img/img_font.png";
break;
case FILE_SCRIPT :
iconFile = m_strAppDir+"/img/img_script.png";
break;
case FILE_THEME :
iconFile = m_strAppDir+"/img/img_theme.png";
break;
case FILE_KM :
case FILE_QKT :
iconFile = m_strAppDir+"/img/img_km.png";
break;
case FILE_IMG :
iconFile = m_strAppDir+"/img/img_picture.png";
break;
case FILE_MEDIA:
iconFile = m_strAppDir+"/img/img_media.png";
break;
case FILE_TEXT :
iconFile = m_strAppDir+"/img/img_text.png";
break;
case FILE_DIR :
iconFile = m_strAppDir+"/img/img_dir.png";
break;
case FILE_SYMLINK:
iconFile = m_strAppDir+"/img/img_symlink.png";
break;
case FILE_OTHER:
default:
iconFile = m_strAppDir+"/img/img_file.png";
break;
}
return QPixmap(iconFile);
}
QString FileBrowser::adjustFile(const QString &file)
{
if(m_strCurDir == "/")
return QString("/%1").arg(file);
else
return QString("%1/%2").arg(m_strCurDir).arg(file);
}
void FileBrowser::selAll()
{
if(m_eMode == NORMAL) return;
for(unsigned int i=0; i<count(); i++)
checkItem(i, true);
}
void FileBrowser::selNone()
{
if(m_eMode == NORMAL) return;
for(unsigned int i=0; i<count(); i++)
checkItem(i, false);
}
void FileBrowser::enableMimeType(bool enable)
{
m_bMimeTypeState = enable;
}
bool FileBrowser::isEnableMimeType()
{
return m_bMimeTypeState;
}
void FileBrowser::setFilter(FILTERTYPE t)
{
m_eFilterType = t;
}
ITEMMODE FileBrowser::getMode()
{
return m_eMode;
}
QString FileBrowser::getCurDir()
{
return m_strCurDir;
}
QString FileBrowser::getFileName()
{
return m_strFileName;
}
QString FileBrowser::getFilePath()
{
return m_strFilePath;
}