aboutsummaryrefslogtreecommitdiff
path: root/lumina-fm/MainUI.cpp
blob: 1305e12df005772093448381eb877d29302e1abc (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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2014-2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "MainUI.h"
#include "ui_MainUI.h"

#include <QFileInfo>

#define DEBUG 0

MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI){
  //for Signal/slot we must register the Typedef of QFileInfoList
  qRegisterMetaType<QFileInfoList>("QFileInfoList");
  qRegisterMetaType< LFileInfoList >("LFileInfoList");
  ui->setupUi(this);
  if(DEBUG){ qDebug() << "Initilization:"; }
  //Be careful about the QSettings setup, it must match the lumina-desktop setup
  QSettings::setPath(QSettings::NativeFormat, QSettings::UserScope, QDir::homePath()+"/.lumina");
  settings = new QSettings( QSettings::UserScope, "LuminaDE", "lumina-fm", this);
  favdir = QDir::homePath()+"/.lumina/favorites/"; //save this for later
  //syncTimer =  new QTimer(this);
    //syncTimer->setInterval(200); //1/5 second (collect as many signals/slots as necessary
    //syncTimer->setSingleShot(true);
  //Reset the UI to the previously used size (if possible)
  if(DEBUG){ qDebug() << " - Reset window size"; }
  int height = settings->value("geometry/height",-1).toInt();
  if(height>100 && height <= QApplication::desktop()->availableGeometry(this).height()){ this->resize(this->width(), height); }
  int width = settings->value("geometry/width",-1).toInt();
  if(width>100 && width <= QApplication::desktop()->availableGeometry(this).width()){ this->resize(width, this->height() ); }
  //initialize the non-ui widgets
  if(DEBUG){ qDebug() << " - Tab Bar Setup"; }
  tabBar = new QTabBar(this);
    tabBar->setTabsClosable(true);
    tabBar->setMovable(true); //tabs are independant - so allow the user to sort them
    tabBar->setShape(QTabBar::RoundedNorth);
    tabBar->setFocusPolicy(Qt::NoFocus);
    static_cast<QBoxLayout*>(ui->centralwidget->layout())->insertWidget(0,tabBar);
  if(DEBUG){ qDebug() << " - Threading"; }
  workThread = new QThread;
    workThread->setObjectName("Lumina-fm filesystem worker");
  worker = new DirData();
    worker->zfsavailable = LUtils::isValidBinary("zfs");
    connect(worker, SIGNAL(DirDataAvailable(QString, QString, LFileInfoList)), this, SLOT(DirDataAvailable(QString, QString, LFileInfoList)) );
    connect(worker, SIGNAL(SnapshotDataAvailable(QString, QString, QStringList)), this, SLOT(SnapshotDataAvailable(QString, QString, QStringList)) );
    worker->moveToThread(workThread);
  if(DEBUG){ qDebug() << " - File System Model"; }
  fsmod = new QFileSystemModel(this);
    fsmod->setRootPath(QDir::homePath());
  dirCompleter = new QCompleter(fsmod, this);
    dirCompleter->setModelSorting( QCompleter::CaseInsensitivelySortedModel );
  if(DEBUG){ qDebug() << " - Context Menu"; }
  contextMenu = new QMenu(this);
  radio_view_details = new QRadioButton(tr("Detailed List"), this);
  radio_view_list = new QRadioButton(tr("Basic List"), this);
  radio_view_tabs = new QRadioButton(tr("Prefer Tabs"), this);
  radio_view_cols = new QRadioButton(tr("Prefer Columns"), this);
  ui->menuView_Mode->clear();
  ui->menuGroup_Mode->clear();
  detWA = new QWidgetAction(this);
    detWA->setDefaultWidget(radio_view_details);
  listWA = new QWidgetAction(this);
    listWA->setDefaultWidget(radio_view_list);
  tabsWA = new QWidgetAction(this);
    tabsWA->setDefaultWidget(radio_view_tabs);
  colsWA = new QWidgetAction(this);
    colsWA->setDefaultWidget(radio_view_cols);
    ui->menuView_Mode->addAction(detWA);
    ui->menuView_Mode->addAction(listWA);
    ui->menuGroup_Mode->addAction(tabsWA);
    ui->menuGroup_Mode->addAction(colsWA);
  //Setup the pages
  //ui->BrowserLayout->clear();
  ui->page_player->setLayout(new QVBoxLayout());
  ui->page_image->setLayout(new QVBoxLayout());
  MW = new MultimediaWidget(this);
  SW = new SlideshowWidget(this);
  ui->page_player->layout()->addWidget(MW);
  ui->page_image->layout()->addWidget(SW);

  //Setup any specialty keyboard shortcuts
  if(DEBUG){ qDebug() << " - Keyboard Shortcuts"; }
  nextTabLShort = new QShortcut( QKeySequence(tr("Shift+Left")), this);
  nextTabRShort = new QShortcut( QKeySequence(tr("Shift+Right")), this);
  closeTabShort = new QShortcut( QKeySequence(tr("Ctrl+W")), this);
  refreshShort = new QShortcut( QKeySequence(tr("F5")), this);

  //Finish loading the interface
  workThread->start();
  if(DEBUG){ qDebug() << " - Icons"; }
  setupIcons();
  if(DEBUG){ qDebug() << " - Connections"; }
  setupConnections();
  if(DEBUG){ qDebug() << " - Settings"; }
  loadSettings();
  if(DEBUG){ qDebug() << " - Bookmarks"; }
  RebuildBookmarksMenu();
  if(DEBUG){ qDebug() << " - Devices"; }
  RebuildDeviceMenu();
  //Make sure we start on the browser page
  if(DEBUG){ qDebug() << " - Load Browser Page"; }
  //goToBrowserPage();
  if(DEBUG){ qDebug() << " - Done with init"; }
}

MainUI::~MainUI(){
  workThread->quit();
  workThread->wait();
}

void MainUI::OpenDirs(QStringList dirs){
  //Now open the dirs
  if(dirs.isEmpty()){ dirs << QDir::homePath(); }
  QStringList invalid;
  for(int i=0; i<dirs.length(); i++){
    if(dirs[i].simplified().isEmpty()){ continue; }
    //Open this directory in a viewer
    if(dirs[i].endsWith("/")){ dirs[i].chop(1); }
    if(!QFile::exists(dirs[i])){ invalid << dirs[i]; continue; }
    if(DEBUG){ qDebug() << "Open Directory:" << dirs[i]; }
    ///Get a new Unique ID
    int id = 0;
    for(int j=0; j<DWLIST.length(); j++){ 
      if(DWLIST[j]->id().section("-",1,1).toInt() >= id){ id = DWLIST[j]->id().section("-",1,1).toInt()+1; }
    }
    //Create the new DirWidget
    DirWidget *DW = new DirWidget("DW-"+QString::number(id), this);
    ui->BrowserLayout->addWidget(DW);
    DWLIST << DW;
    //Connect the signals/slots for it
    connect(DW, SIGNAL(OpenDirectories(QStringList)), this, SLOT(OpenDirs(QStringList)) );
    connect(DW, SIGNAL(LoadDirectory(QString, QString)), worker, SLOT(GetDirData(QString, QString)) );
    connect(DW, SIGNAL(findSnaps(QString, QString)), worker, SLOT(GetSnapshotData(QString, QString)) );
    connect(DW, SIGNAL(PlayFiles(LFileInfoList)), this, SLOT(OpenPlayer(LFileInfoList)) );
    connect(DW, SIGNAL(ViewFiles(LFileInfoList)), this, SLOT(OpenImages(LFileInfoList)) );
    connect(DW, SIGNAL(LaunchTerminal(QString)), this, SLOT(OpenTerminal(QString)) );
    connect(DW, SIGNAL(CutFiles(QStringList)), this, SLOT(CutFiles(QStringList)) );
    connect(DW, SIGNAL(CopyFiles(QStringList)), this, SLOT(CopyFiles(QStringList)) );
    connect(DW, SIGNAL(FavoriteFiles(QStringList)), this, SLOT(FavoriteFiles(QStringList)) );
    connect(DW, SIGNAL(RenameFiles(QStringList)), this, SLOT(RenameFiles(QStringList)) );
    connect(DW, SIGNAL(RemoveFiles(QStringList)), this, SLOT(RemoveFiles(QStringList)) );
    connect(DW, SIGNAL(PasteFiles(QString,QStringList)), this, SLOT(PasteFiles(QString, QStringList)) );
    connect(DW, SIGNAL(CloseBrowser(QString)), this, SLOT(CloseBrowser(QString)) );
    //Now create the tab for this 
    if(radio_view_tabs->isChecked()){
      int index = tabBar->addTab( LXDG::findIcon("folder-open",""), dirs[i].section("/",-1) );
      tabBar->setTabWhatsThis( index, "DW-"+QString::number(id) );
      tabBar->setCurrentIndex(index);
    }else if(tabBar->count()<1){
      //Need to create the generic Browser tab
      int index = tabBar->addTab( LXDG::findIcon("folder-open",""), "Browser" );
      tabBar->setTabWhatsThis( index, "browser" );
      tabBar->setCurrentIndex(index);
    }
    
    //Initialize the widget with the proper settings
    DW->setShowDetails(radio_view_details->isChecked());
    DW->setShowSidebar(ui->actionShow_Action_Buttons->isChecked());
    QList<DirWidget::DETAILTYPES> details; details <<DirWidget::NAME << DirWidget::SIZE << DirWidget::TYPE << DirWidget::DATEMOD;
    DW->setDetails(details); //Which details to show and in which order (L->R)
    DW->setShowThumbnails(ui->actionShow_Thumbnails->isChecked());
    DW->setThumbnailSize(settings->value("iconsize", 32).toInt());
    DW->setDirCompleter(dirCompleter);
    DW->setShowCloseButton(!radio_view_tabs->isChecked());
    //Now load the directory
    DW->ChangeDir(dirs[i]); //kick off loading the directory info
  }
  //Update visibilities
  tabChanged(tabBar->currentIndex());
  tabBar->setVisible( tabBar->count() > 1 );
  if(!invalid.isEmpty()){
    QMessageBox::warning(this, tr("Invalid Directories"), tr("The following directories are invalid and could not be opened:")+"\n"+invalid.join(", ") );
  }
  //Double check that there is at least 1 dir loaded
  //qDebug() << "OpenDirs:" << DWLIST.length() << dirs << invalid << tabBar->currentIndex();
  if(DWLIST.isEmpty()){ OpenDirs(QStringList()); }
  
}

void MainUI::setupIcons(){
  this->setWindowIcon( LXDG::findIcon("Insight-FileManager","") );
	
  //Setup all the icons using libLumina
  ui->actionClose->setIcon( LXDG::findIcon("application-exit","") );
  ui->actionNew_Tab->setIcon( LXDG::findIcon("tab-new-background","") );
  //ui->action_Preferences->setIcon( LXDG::findIcon("configure","") );
  ui->actionManage_Bookmarks->setIcon( LXDG::findIcon("bookmarks-organize","") );
  ui->actionScan->setIcon( LXDG::findIcon("system-search","") );
  ui->actionSearch->setIcon( LXDG::findIcon("edit-find","") );
  ui->actionLarger_Icons->setIcon( LXDG::findIcon("zoom-in","") );
  ui->actionSmaller_Icons->setIcon( LXDG::findIcon("zoom-out", "") );
  //Menu Icons
  ui->menuView_Mode->setIcon( LXDG::findIcon("view-choose","") );
  ui->menuGroup_Mode->setIcon( LXDG::findIcon("tab-duplicate","") );
  
}

//==========
//    PRIVATE
//==========
void MainUI::setupConnections(){
  connect(tabBar, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)) );
  connect(tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(tabClosed(int)) );
  connect(ui->menuBookmarks, SIGNAL(triggered(QAction*)), this, SLOT(goToBookmark(QAction*)) );
  connect(ui->menuExternal_Devices, SIGNAL(triggered(QAction*)), this, SLOT(goToDevice(QAction*)) );
	
  //Radio Buttons
  connect(radio_view_details, SIGNAL(toggled(bool)), this, SLOT(viewModeChanged(bool)) );
  connect(radio_view_list, SIGNAL(toggled(bool)), this, SLOT(viewModeChanged(bool)) );
  connect(radio_view_tabs, SIGNAL(toggled(bool)), this, SLOT(groupModeChanged(bool)) );
  connect(radio_view_cols, SIGNAL(toggled(bool)), this, SLOT(groupModeChanged(bool)) );

  //Special Keyboard Shortcuts
  connect(nextTabLShort, SIGNAL(activated()), this, SLOT( prevTab() ) );
  connect(nextTabRShort, SIGNAL(activated()), this, SLOT( nextTab() ) );
  connect(closeTabShort, SIGNAL(activated()), this, SLOT( tabClosed() ) );
  connect(refreshShort , SIGNAL(activated()), this, SLOT( refreshTabs() ) );
  

}

void MainUI::loadSettings(){
  //Note: make sure this is run after all the UI elements are created and connected to slots
  // but before the first directory gets loaded
  ui->actionView_Hidden_Files->setChecked( settings->value("showhidden", false).toBool() );
    on_actionView_Hidden_Files_triggered(); //make sure to update the models too
  ui->actionShow_Action_Buttons->setChecked(settings->value("showactions", true).toBool() );
    on_actionShow_Action_Buttons_triggered(); //make sure to update the UI
  ui->actionShow_Thumbnails->setChecked( settings->value("showthumbnails", true).toBool() );
  //View Type
  bool showDetails = (settings->value("viewmode","details").toString()=="details");
  if(showDetails){ radio_view_details->setChecked(true); }
  else{ radio_view_list->setChecked(true); }
  //Grouping type
  bool usetabs = (settings->value("groupmode","tabs").toString()=="tabs");
  if(usetabs){ radio_view_tabs->setChecked(true); }
  else{ radio_view_cols->setChecked(true); }
  
}

void MainUI::RebuildBookmarksMenu(){
  //Create the bookmarks menu
  ui->menuBookmarks->clear();
    ui->menuBookmarks->addAction(ui->actionManage_Bookmarks);
    ui->menuBookmarks->addSeparator();
  QStringList BM = settings->value("bookmarks", QStringList()).toStringList();
  ui->menuBookmarks->clear();
    ui->menuBookmarks->addAction(ui->actionManage_Bookmarks);
    ui->menuBookmarks->addSeparator();
  bool changed = false;
  BM.sort(); //Sort alphabetically
  for(int i=0; i<BM.length(); i++){
    if(QFile::exists(BM[i].section("::::",1,1)) ){
      QAction *act = new QAction(BM[i].section("::::",0,0),this);
        act->setWhatsThis(BM[i].section("::::",1,1));
      ui->menuBookmarks->addAction(act);
    }else{
      //Invalid directory - remove the bookmark
      BM.removeAt(i);
      i--;
      changed = true;
    }
  }
  if(changed){ settings->setValue("bookmarks",BM); }
  ui->actionManage_Bookmarks->setEnabled(BM.length()>0);
}

void MainUI::RebuildDeviceMenu(){
  //Create the External Devices Menu appropriately
  ui->menuExternal_Devices->clear();
    ui->menuExternal_Devices->addAction( ui->actionScan );
    ui->menuExternal_Devices->addSeparator();
  //Scan for externally mounted devices
  QStringList devs = LOS::ExternalDevicePaths();
    //Output Format: <type>::::<filesystem>::::<path>  (6/24/14 - version 0.4.0 )
        // <type> = [USB, HDRIVE, SDCARD, DVD, LVM, UNKNOWN]
  qDebug() << "Externally-mounted devices:" << devs;
  //Now add them to the menu appropriately
  for(int i=0; i<devs.length(); i++){
    //Skip hidden mount points (usually only for system usage - not user browsing)
    QString label, path, fs;
      //Format inputs as necesary
      path = devs[i].section("::::",2,2);
      fs = devs[i].section("::::",1,1);
      if(path.endsWith("/") && path.length()>1 ){ path.chop(1); }
      if(path == "/"){ label = tr("Root"); }
      else{ label = path.section("/",-1).simplified(); }
    if(label.startsWith(".") ){ continue; }  //don't show hidden mountpoint (not usually user-browsable)
    //Create entry for this device
    if( !fs.simplified().isEmpty()){
      //Add filesystem type to the label
      label = QString(tr("%1 (Type: %2)")).arg(label, fs);
    }
    QAction *act = new QAction(label,this);          
        act->setWhatsThis(path); //full path to mountpoint
	act->setToolTip( QString(tr("Filesystem: %1")).arg( devs[i].section("::::",1,1) ) );
	//Now set the appropriate icon
	QString type = devs[i].section("::::",0,0);
	if(type=="USB"){ type = "drive-removable-media-usb"; }
	else if(type=="HDRIVE" || type=="LVM"){ type = "drive-harddisk"; }
	else if(type=="SDCARD"){ type = "media-flash-sd-mmc"; }
	else if(type=="DVD"){ type = "media-optical"; }
	else{ type = "drive-removable-media"; }
	act->setIcon( LXDG::findIcon(type, "") );
      ui->menuExternal_Devices->addAction(act);
  }
}

DirWidget* MainUI::FindActiveBrowser(){
  //Find the current directory
  DirWidget *curB = 0;
  //Get the current tab ID to start with
  QString cur = tabBar->tabWhatsThis(tabBar->currentIndex());
  //if(cur.startsWith("#")){ cur.clear(); } //multimedia/player tab open
  
  if(DWLIST.length()==1){
    //Only 1 browser open - use it
    curB = DWLIST[0];
  }else if(cur.startsWith("DW-")){
    //This is a tab for a browser - just find the matching widget
    for(int i=0; i<DWLIST.length(); i++){
      if(DWLIST[i]->id()==cur){ curB = DWLIST[i]; break; }
    }
  }else{
    //This is a bit more complex - either showing multiple columns or a non-browser tab is active
    if(cur=="Browser"){
      //Column View
      QWidget *focus = QApplication::focusWidget(); //the widget which currently has focus
      for(int i=0; i<DWLIST.length(); i++){
        if(DWLIST[i]->isAncestorOf(focus)){ curB = DWLIST[i]; break; } //This browser has focus
      }
	    
    }else{
      //Non-Browser in focus - use the fallback below
    }
  }
  //if all else fails - just use the first browser in the list (there should always be at least one)
  if(curB==0 && !DWLIST.isEmpty()){ curB = DWLIST[0];  }
  return curB;
}

//==============
//    PRIVATE SLOTS
//==============
void MainUI::DisplayStatusBar(QString msg){
	//qDebug() << "message to show in the status bar:" << msg;
	ui->statusbar->showMessage(msg);
}
	
//---------------------
//Menu Actions
//---------------------
void MainUI::on_actionNew_Tab_triggered(){
  OpenDirs(QStringList() << QDir::homePath());
}

void MainUI::on_actionSearch_triggered(){
  DirWidget *dir = FindActiveBrowser();
  if(dir==0){ return; }
  QProcess::startDetached("lumina-search -dir \""+dir->currentDir()+"\"");
}

void MainUI::on_actionClose_triggered(){
  if(tabBar->count() > 1){
    if(QMessageBox::Yes != QMessageBox::question(this, tr("Verify Quit"), tr("You have multiple tabs open. Are you sure you want to quit?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes ) ){
      return;
    }
  }
  qDebug() << "Closing Down...";
  this->close();
}

void MainUI::on_actionView_Hidden_Files_triggered(){
  worker->showHidden = ui->actionView_Hidden_Files->isChecked();
  //Now save this setting for later
  settings->setValue("showhidden", ui->actionView_Hidden_Files->isChecked());
  worker->showHidden = ui->actionView_Hidden_Files->isChecked();
  //Re-load the current browsers
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->refresh(); }

}

void MainUI::on_actionShow_Action_Buttons_triggered(){
  bool show = ui->actionShow_Action_Buttons->isChecked();
  settings->setValue("showactions", show);
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->setShowSidebar(show); }
}

void MainUI::on_actionShow_Thumbnails_triggered(){
  //Now save this setting for later
  bool show = ui->actionShow_Thumbnails->isChecked();
  settings->setValue("showthumbnails", show);
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->setShowThumbnails(show); }
}

void MainUI::goToBookmark(QAction *act){
  if(act==ui->actionManage_Bookmarks){
    BMMDialog dlg(this);
      dlg.loadSettings(settings);
      dlg.exec();
    RebuildBookmarksMenu();
  }else{
    //Find the current directory
    DirWidget *dir = FindActiveBrowser();
    if(dir!=0){ 
      dir->ChangeDir(act->whatsThis());
      return;
    }
    //If no current dir could be found - open a new tab/column
    OpenDirs(QStringList() << act->whatsThis() );
  }
}

void MainUI::goToDevice(QAction *act){
  if(act==ui->actionScan){
    RebuildDeviceMenu();
  }else{
    DirWidget *dir = FindActiveBrowser();
    if(dir!=0){ 
      dir->ChangeDir(act->whatsThis());
      return;
    }
    //If no current dir could be found - open a new tab/column
    OpenDirs(QStringList() << act->whatsThis() );
  }
}

void MainUI::viewModeChanged(bool active){
  if(!active){ return; } //on every view change, all radio buttons will call this function - only run this once though
  bool showDetails = radio_view_details->isChecked();
  if(showDetails){ settings->setValue("viewmode","details"); }
  else{ settings->setValue("viewmode","list"); }

  //Re-load the view widgets
  for(int i=0; i<DWLIST.length(); i++){
    DWLIST[i]->setShowDetails(showDetails);
  }
	
}

void MainUI::groupModeChanged(bool active){
  if(!active){ return; } //on every change, all radio buttons will call this function - only run this once though
  bool usetabs = radio_view_tabs->isChecked();
  if(usetabs){ 
    settings->setValue("groupmode","tabs"); 
    //Now clean up all the tabs (remove the generic one and add the specific ones)
    for(int i=0; i<tabBar->count(); i++){
      //Remove all the browser tabs
      if( !tabBar->tabWhatsThis(i).startsWith("#") ){
	tabBar->removeTab(i);
	i--; //go back one to ensure nothing is missed
      }
    }
    //Create all the specific browser tabs for open browsers
    for(int i=0; i<DWLIST.length(); i++){
      qDebug() << "Add specific tab:" << DWLIST[i]->currentDir() << DWLIST[i]->id();
      int tab = tabBar->addTab( LXDG::findIcon("folder-open",""), DWLIST[i]->currentDir().section("/",-1) );
      tabBar->setTabWhatsThis(tab, DWLIST[i]->id() );
      DWLIST[i]->setShowCloseButton(false);
    }
  }else{
    settings->setValue("groupmode","columns");
    //Now clean up the tabs (remove the specific ones and add a generic one)
    for(int i=0; i<tabBar->count(); i++){
      //Remove all the browser tabs
      if( !tabBar->tabWhatsThis(i).startsWith("#") ){
	tabBar->removeTab(i);
	i--; //go back one to ensure nothing is missed
      }
    }
    //Now create the generic "browser" tab
    int tab = tabBar->addTab( LXDG::findIcon("folder-open",""), tr("Browser") );
      tabBar->setTabWhatsThis(tab, "Browser" );
    for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->setShowCloseButton(true); }
  }
  if(tabBar->currentIndex()<0){ tabBar->setCurrentIndex(0); }
  tabBar->setVisible( tabBar->count() > 1 );
  QTimer::singleShot(20, this, SLOT(tabChanged()) );
}

void MainUI::on_actionLarger_Icons_triggered(){
  int size = settings->value("iconsize", 32).toInt();
  size += 16;
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->setThumbnailSize(size); }
  settings->setValue("iconsize", size);
}

void MainUI::on_actionSmaller_Icons_triggered(){
  int size = settings->value("iconsize", 32).toInt();
  if(size <= 16){ return; }
  size -= 16;
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->setThumbnailSize(size); }
  settings->setValue("iconsize", size);	
}

/*void MainUI::on_actionBookMark_triggered(){
  QString dir = getCurrentDir();
  bool ok = false;
  QString name = QInputDialog::getText(this, tr("New Bookmark"), tr("Name:"), QLineEdit::Normal, dir, \
		&ok, 0, Qt::ImhFormattedNumbersOnly | Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly);
  if(!ok || name.isEmpty()){ return; } //cancelled
  QStringList BM = settings->value("bookmarks",QStringList()).toStringList();
  if(BM.filter(name+"::::").length() >0){
    QMessageBox::warning(this, tr("Invalid Name"), tr("This bookmark name already exists. Please choose another.") );
    QTimer::singleShot(0,this, SLOT(on_actionBookMark_triggered()));
    return;
  }
  BM.append(name+"::::"+dir);
  BM.sort(); //sort alphabetically by name
  settings->setValue("bookmarks", BM);
  //Now rebuild the bookmarks menu
  RebuildBookmarksMenu();
  ui->actionBookMark->setEnabled(false); //already bookmarked
}*/

void MainUI::tabChanged(int tab){
  if(tab<0){ tab = tabBar->currentIndex(); }
  if(tab < 0){ return; }
  //Load the directory contained in the new tab
  QString info = tabBar->tabWhatsThis(tab); //get the full directory
  if(info.isEmpty()){ return; } //unknown tab (this happens while we are in the middle of making changes to tabs - just ignore it)
  //qDebug() << "Change to Tab:" << tab << tabBar->tabText(tab);
  //qDebug() << " -- ID:" << info;
  if(info=="#MW"){ ui->stackedWidget->setCurrentWidget(ui->page_player); }
  else if(info=="#SW"){ ui->stackedWidget->setCurrentWidget(ui->page_image); }
  else{
    ui->stackedWidget->setCurrentWidget(ui->page_browser);
    if(radio_view_tabs->isChecked()){
      for(int i=0; i<DWLIST.length(); i++){
	 DWLIST[i]->setVisible(DWLIST[i]->id()==info);     
      }
    }else{
      //For columns, all widgets need to be visible
      for(int i=0; i<DWLIST.length(); i++){
	 DWLIST[i]->setVisible(true);     
      }	    
    }
  }
  tabBar->setVisible( tabBar->count() > 1 );
}

void MainUI::tabClosed(int tab){
  if(tabBar->count()==1){ return; } //Can't close the only tab
  if(tab < 0){ tab = tabBar->currentIndex(); }
  QString info = tabBar->tabWhatsThis(tab);
   if(!info.startsWith("#")){ 
    if(DWLIST.length()<2){ return; }
    for(int i=0; i<DWLIST.length(); i++){
      if(info == DWLIST[i]->id()){
        delete DWLIST.takeAt(i);
	break;
      }
    }
   }else if(info=="#MW"){
      MW->Cleanup(); //prepare it to be hidden/removed
   }
  //Remove the tab (will automatically move to a different one);
  qDebug() << "Closing tab:" << tab << tabBar->tabText(tab);
  tabBar->removeTab(tab);
  tabBar->setVisible( tabBar->count() > 1 );
}

void MainUI::prevTab(){
  int cur = tabBar->currentIndex();
  if(cur == 0){ tabBar->setCurrentIndex( tabBar->count()-1 ); }
  else{ tabBar->setCurrentIndex( cur-1 ); }
}

void MainUI::nextTab(){
  int cur = tabBar->currentIndex();
  if(cur == (tabBar->count()-1) ){ tabBar->setCurrentIndex(0); }
  else{ tabBar->setCurrentIndex( cur+1 ); }	
}

void MainUI::refreshTabs(){
  DirWidget *cur = FindActiveBrowser();
  if(cur!=0){ cur->refresh(); }
}

void MainUI::DirDataAvailable(QString id, QString dir, LFileInfoList list){
  for(int i=0; i<DWLIST.length(); i++){
    if(id == DWLIST[i]->id()){
      DWLIST[i]->LoadDir(dir, list);
      break;
    }
  }
  if(radio_view_tabs->isChecked()){
    //Need to update the text for the tab so it corresponds to the current directory loaded
    for(int i=0; i<tabBar->count(); i++){
      if(tabBar->tabWhatsThis(i)==id){
        tabBar->setTabText(i, dir.section("/",-1));
      }
    }
  }
}

void MainUI::SnapshotDataAvailable(QString id , QString dir, QStringList list){
  for(int i=0; i<DWLIST.length(); i++){
    if(id == DWLIST[i]->id()){
      DWLIST[i]->LoadSnaps(dir, list);
      break;
    }
  }
}

void MainUI::OpenPlayer(LFileInfoList list){
  //See if the tab is available for the multimedia player
  int tab = -1;
  for(int i=0; i<tabBar->count(); i++){
    if(tabBar->tabWhatsThis(i)=="#MW"){ tab=i; break;}
  }
  if(tab<0){
    //Need to create a new tab
    tab = tabBar->addTab(LXDG::findIcon("media-playback-start",""), tr("Multimedia"));
    tabBar->setTabWhatsThis(tab,"#MW");
    //Also clear the info in the player
    MW->ClearPlaylist();
  }
  //Load the data into the player
  MW->LoadMultimedia(list);
  //Switch to the player tab
  tabBar->setCurrentIndex(tab);
}

void MainUI::OpenImages(LFileInfoList list){
  int tab = -1;
  for(int i=0; i<tabBar->count(); i++){
    if(tabBar->tabWhatsThis(i)=="#SW"){ tab=i; break;}
  }
  if(tab<0){
    //Need to create a new tab
    tab = tabBar->addTab(LXDG::findIcon("fileview-preview",""), tr("Slideshow"));
    tabBar->setTabWhatsThis(tab,"#SW");
    //Also clear the info in the viewer
    SW->ClearImages();
  }
  //Load the data into the viewer
  SW->LoadImages(list);
  //Switch to the player tab
  tabBar->setCurrentIndex(tab);
  QTimer::singleShot(20, SW, SLOT(refresh()) );
}

void MainUI::OpenTerminal(QString dirpath){
  //we use the application defined as the default terminal 
  QSettings sessionsettings( QSettings::UserScope, "LuminaDE","sessionsettings", this);
  //xterm remains the default
  QString defTerminal = sessionsettings.value("default-terminal", "xterm").toString();
  //qDebug() << "Found default terminal:" << defTerminal;
  //Now get the exec string and run it
  QString cmd = LUtils::GenerateOpenTerminalExec(defTerminal, dirpath);
  //qDebug() << "Starting Terminal with command:" << cmd;
  QProcess::startDetached(cmd);

}

void MainUI::CutFiles(QStringList list){
  qDebug() << "Cut Files:" << list;
  if(list.isEmpty()){ return; } //nothing selected
  //Format the data string
  QList<QUrl> urilist; //Also assemble a URI list for cros-app compat (no copy/cut distinguishing)
  for(int i=0; i<list.length(); i++){
    urilist << QUrl::fromLocalFile(list[i]);
    list[i] = list[i].prepend("cut::::");
  }
  //Now save that data to the global clipboard
  QMimeData *dat = new QMimeData;
	dat->clear();
	dat->setData("x-special/lumina-copied-files", list.join("\n").toLocal8Bit());
	dat->setUrls(urilist); //the text/uri-list mimetype - built in Qt conversion/use
  QApplication::clipboard()->clear();
  QApplication::clipboard()->setMimeData(dat);
  //Update all the buttons to account for clipboard change
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->refreshButtons(); }
}

void MainUI::CopyFiles(QStringList list){
  qDebug() << "Copy Files:" << list;
  if(list.isEmpty()){ return; } //nothing selected
  //Format the data string
  QList<QUrl> urilist; //Also assemble a URI list for cros-app compat (no copy/cut distinguishing)
  for(int i=0; i<list.length(); i++){
    urilist << QUrl::fromLocalFile(list[i]);
    list[i] = list[i].prepend("copy::::");
  }
  //Now save that data to the global clipboard
  QMimeData *dat = new QMimeData;
	dat->clear();
	dat->setData("x-special/lumina-copied-files", list.join("\n").toLocal8Bit());
	dat->setUrls(urilist); //the text/uri-list mimetype - built in Qt conversion/use
  QApplication::clipboard()->clear();
  QApplication::clipboard()->setMimeData(dat);
  //Update all the buttons to account for clipboard change
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->refreshButtons(); }
}

void MainUI::PasteFiles(QString dir, QStringList raw){
  qDebug() << "Paste Files:" << dir;
  QStringList cut, copy, newcut, newcopy;
  if(raw.isEmpty()){
    //Pull info from the clipboard
    const QMimeData *dat = QApplication::clipboard()->mimeData();
    raw = QString(dat->data("x-special/lumina-copied-files")).split("\n");
  }
  if(!dir.endsWith("/")){ dir.append("/"); }
  for(int i=0; i<raw.length(); i++){
    if(raw[i].startsWith("cut::::")){ 
	cut << raw[i].section("::::",1,50);
	newcut << dir+raw[i].section("::::",1,50).section("/",-1);
    }
    else if(raw[i].startsWith("copy::::")){ 
	copy << raw[i].section("::::",1,50); 
	newcopy<< dir+raw[i].section("::::",1,50).section("/",-1);
    }
  }
  bool errs = false;
  //Perform the copy/move operations
  if(!copy.isEmpty()){ 
    qDebug() << "Paste Copy:" << copy << "->" << newcopy;
    FODialog dlg(this);
      if( !dlg.CopyFiles(copy, newcopy) ){ return; } //cancelled
      dlg.show();
      dlg.exec();
      errs = errs || !dlg.noerrors;
  }
  if(!cut.isEmpty()){
    qDebug() << "Paste Cut:" << cut << "->" << newcut;
    FODialog dlg(this);
      if(!dlg.MoveFiles(cut, newcut) ){ return; } //cancelled
      dlg.show();
      dlg.exec();
      errs = errs || !dlg.noerrors;
  }
  //Modify the clipboard appropriately
  if(!errs && !cut.isEmpty()){
    //Now clear the clipboard since those old file locations are now invalid
    QApplication::clipboard()->clear(); 
    if(!copy.isEmpty()){
      //There were also files copied: save those files back into the clipboard
       QMimeData *dat = new QMimeData;
	dat->clear();
	dat->setData("x-special/lumina-copied-files", raw.filter("copy::::").join("\n").toLocal8Bit());
	QApplication::clipboard()->setMimeData(dat);
    }
  }
  //Update all the buttons to account for clipboard change
  for(int i=0; i<DWLIST.length(); i++){ DWLIST[i]->refreshButtons(); }
}

void MainUI::FavoriteFiles(QStringList list){
  qDebug() << "Favorite Files:" << list;
  for(int i=0; i<list.length(); i++){
    LUtils::addFavorite(list[i]);
  }
  //Might want to make this a "toggle" instead of an add later on...
}

void MainUI::RenameFiles(QStringList list){
  qDebug() << "Rename Files:" << list;
  for(int i=0; i<list.length(); i++){
    QString fname = list[i];
    QString path = fname;
    fname = fname.section("/",-1); //turn this into just the file name
    path.chop(fname.length()); 	//turn this into the base directory path (has a "/" at the end)
    //Now prompt for the new filename
    bool ok = false;
    QString nname = QInputDialog::getText(this, tr("Rename File"),tr("New Name:"), QLineEdit::Normal, fname, &ok);
    if(!ok || nname.isEmpty()){ return; } //cancelled
    //Now check for a file extension and add it if necessary
    QString oext = fname.section(".",-1);
      if("."+oext == fname){ oext.clear(); } //hidden file without an extension
      else if(oext==fname){ oext.clear(); } //no extension
    QString next = nname.section(".",-1);
      if(next==nname){ next.clear(); } //no extension
    if(next.isEmpty() && !oext.isEmpty()){
      nname.append( "."+oext );
    }
    //Check if this filename already exists
    bool overwrite = QFile::exists(path+nname);
    if(overwrite){
      if(QMessageBox::Yes != QMessageBox::question(this, tr("Overwrite File?"), tr("An existing file with the same name will be replaced. Are you sure you want to proceed?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ){
        return; //cancelled
      }
    }
    //Now perform the move
    qDebug() << "Rename:" << path+fname << "->" << path+nname;
    FODialog dlg(this);
      dlg.setOverwrite(overwrite);
      dlg.MoveFiles(QStringList() << path+fname, QStringList() << path+nname);
      dlg.show();
      dlg.exec();
  } //end loop over list of files
}

void MainUI::RemoveFiles(QStringList list){
  if(list.isEmpty()){ return; } //nothing selected	
  qDebug() << "Remove Files:" << list;
  QStringList paths, names;
  for(int i=0; i<list.length(); i++){
     paths << list[i];
     names << list[i].section("/",-1);
   }
  
  //Verify permanent removal of file/dir
  QMessageBox dlgQ(QMessageBox::Question, tr("Verify Removal"), tr("WARNING: This will permanently delete the file(s) from the system!")+"\n"+tr("Are you sure you want to continue?"), QMessageBox::Yes | QMessageBox::No, this);
    dlgQ.setDetailedText(tr("Items to be removed:")+"\n\n"+names.join("\n"));
  dlgQ.exec();
  if(dlgQ.result() != QMessageBox::Yes){ return; } //cancelled   

  //Now remove the file/dir
  qDebug() << " - Delete: "<<paths;
  FODialog dlg(this);
    dlg.RemoveFiles(paths);
    dlg.show();
    dlg.exec();
}

void MainUI::CloseBrowser(QString ID){
  //Find the tab associated with this browser first
  for(int i=0; i<tabBar->count(); i++){
    if(tabBar->tabWhatsThis(i)==ID){
      tabBar->removeTab(i);
      break;
    }
  }
  //Now remove the browser itself
  for(int i=0; i<DWLIST.length(); i++){
    if(DWLIST[i]->id()==ID){
      delete DWLIST.takeAt(i);
      break;
    }
  }
  //If the last browser was just closed, create a new one
  if(DWLIST.isEmpty()){
    OpenDirs(QStringList() << QDir::homePath());
  }
}
// ======================

void MainUI::resizeEvent(QResizeEvent *event){
  //Save the new size internally
  settings->setValue("geometry/height", event->size().height());
  settings->setValue("geometry/width", event->size().width());
}
bgstack15