1 //===========================================
2 //  Lumina Desktop Source Code
3 //  Copyright (c) 2016, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 #include "page_autostart.h"
8 #include "ui_page_autostart.h"
9 
10 #include "../AppDialog.h"
11 //==========
12 //    PUBLIC
13 //==========
page_autostart(QWidget * parent)14 page_autostart::page_autostart(QWidget *parent) : PageWidget(parent), ui(new Ui::page_autostart()){
15   ui->setupUi(this);
16   ui->list_session_start->setMouseTracking(true);
17   updateIcons();
18   connect(ui->tool_session_addapp, SIGNAL(clicked()), this, SLOT(addsessionstartapp()) );
19   connect(ui->tool_session_addbin, SIGNAL(clicked()), this, SLOT(addsessionstartbin()) );
20   connect(ui->tool_session_addfile, SIGNAL(clicked()), this, SLOT(addsessionstartfile()) );
21   connect(ui->list_session_start, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(settingChanged()) );
22 }
23 
~page_autostart()24 page_autostart::~page_autostart(){
25 
26 }
27 
28 
29 
30 //================
31 //    PUBLIC SLOTS
32 //================
SaveSettings()33 void page_autostart::SaveSettings(){
34   //qDebug() << "Load AutoStart Files for saving";
35   QList<XDGDesktop*> STARTAPPS = LXDG::findAutoStartFiles(true); //also want invalid/disabled items
36   //qDebug() << " - done";
37   //bool newstartapps = false;
38   for(int i=0; i<ui->list_session_start->count(); i++){
39     QString file = ui->list_session_start->item(i)->whatsThis();
40     bool enabled = ui->list_session_start->item(i)->checkState()==Qt::Checked;
41     bool found = false;
42     for(int i=0; i<STARTAPPS.length(); i++){
43       if(STARTAPPS[i]->filePath==file){
44         found = true;
45 	if(enabled != !STARTAPPS[i]->isHidden){
46 	  //value is different
47 	  qDebug() << "Setting Autostart:" << enabled << STARTAPPS[i]->filePath;
48 	  STARTAPPS[i]->setAutoStarted(enabled);
49 	}
50 	break;
51       }
52     }
53     if(!found && enabled){
54       //New file/binary/app
55       qDebug() << "Adding new AutoStart File:" << file;
56       LXDG::setAutoStarted(enabled, file);
57       //newstartapps = true;
58     }
59   } //end loop over GUI items
60   //Now cleanup all the STARTAPPS data
61   for(int i=STARTAPPS.length()-1; i>=0; i--){ STARTAPPS[i]->deleteLater(); }
62 }
63 
LoadSettings(int)64 void page_autostart::LoadSettings(int){
65   emit HasPendingChanges(false);
66   emit ChangePageTitle( tr("Startup Services") );
67   //qDebug() << "Load AutoStart Files";
68   QList<XDGDesktop*> STARTAPPS = LXDG::findAutoStartFiles(true); //also want invalid/disabled items
69   //qDebug() << " - done:" << STARTAPPS.length();
70   //qDebug() << "StartApps:";
71   ui->list_session_start->clear();
72   for(int i=0; i<STARTAPPS.length(); i++){
73   //qDebug() << STARTAPPS[i]->filePath +" -> " +STARTAPPS[i]->name << STARTAPPS[i]->isHidden;
74     if( !STARTAPPS[i]->isValid() || !QFile::exists(STARTAPPS[i]->filePath) ){ continue; }
75     QListWidgetItem *it = new QListWidgetItem( LXDG::findIcon(STARTAPPS[i]->icon,"application-x-executable"), STARTAPPS[i]->name );
76 	it->setWhatsThis(STARTAPPS[i]->filePath); //keep the file location
77         it->setToolTip(STARTAPPS[i]->comment);
78 	if(STARTAPPS[i]->isHidden){ it->setCheckState( Qt::Unchecked); }
79 	else{it->setCheckState( Qt::Checked); }
80 	ui->list_session_start->addItem(it);
81   }
82   //Now cleanup all the STARTAPPS data
83   for(int i=STARTAPPS.length()-1; i>=0; i--){ STARTAPPS[i]->deleteLater(); }
84 }
85 
updateIcons()86 void page_autostart::updateIcons(){
87   ui->tool_session_addapp->setIcon( LXDG::findIcon("system-run","") );
88   ui->tool_session_addbin->setIcon( LXDG::findIcon("system-search","") );
89   ui->tool_session_addfile->setIcon( LXDG::findIcon("quickopen-file","") );
90 }
91 
92 //=================
93 //         PRIVATE
94 //=================
getSysApp(bool allowreset)95 QString page_autostart::getSysApp(bool allowreset){
96   AppDialog dlg(this);
97     dlg.allowReset(allowreset);
98     dlg.exec();
99   if(dlg.appreset && allowreset){
100     return "reset";
101   }else{
102     return dlg.appselected;
103   }
104 }
105 
106 //=================
107 //    PRIVATE SLOTS
108 //=================
rmsessionstartitem()109 void page_autostart::rmsessionstartitem(){
110   if(ui->list_session_start->currentRow() < 0){ return; } //no item selected
111   delete ui->list_session_start->takeItem(ui->list_session_start->currentRow());
112     settingChanged();
113 }
114 
addsessionstartapp()115 void page_autostart::addsessionstartapp(){
116   //Prompt for the application to start
117   QString app = getSysApp(false); //no reset
118   if(app.isEmpty()){ return; } //cancelled
119   XDGDesktop desk(app);
120   QListWidgetItem *it = new QListWidgetItem( LXDG::findIcon(desk.icon,""), desk.name );
121     it->setWhatsThis(desk.filePath);
122     it->setToolTip(desk.comment);
123     it->setCheckState(Qt::Checked);
124 
125   ui->list_session_start->addItem(it);
126   ui->list_session_start->setCurrentItem(it);
127     settingChanged();
128 }
129 
addsessionstartbin()130 void page_autostart::addsessionstartbin(){
131   QString chkpath = LOS::AppPrefix() + "bin";
132   if(!QFile::exists(chkpath)){ chkpath = QDir::homePath(); }
133   QString bin = QFileDialog::getOpenFileName(this, tr("Select Binary"), chkpath, tr("Application Binaries (*)") );
134   if( bin.isEmpty() || !QFile::exists(bin) ){ return; } //cancelled
135   if( !QFileInfo(bin).isExecutable() ){
136     QMessageBox::warning(this, tr("Invalid Binary"), tr("The selected file is not executable!"));
137     return;
138   }
139   QListWidgetItem *it = new QListWidgetItem( LXDG::findIcon("application-x-executable",""), bin.section("/",-1) );
140     it->setWhatsThis(bin); //command to be saved/run
141     it->setToolTip(bin);
142     it->setCheckState(Qt::Checked);
143   ui->list_session_start->addItem(it);
144   ui->list_session_start->setCurrentItem(it);
145   settingChanged();
146 }
147 
addsessionstartfile()148 void page_autostart::addsessionstartfile(){
149   QString chkpath = QDir::homePath();
150   QString bin = QFileDialog::getOpenFileName(this, tr("Select File"), chkpath, tr("All Files (*)") );
151   if( bin.isEmpty() || !QFile::exists(bin) ){ return; } //cancelled
152   QListWidgetItem *it = new QListWidgetItem( LXDG::findMimeIcon(bin), bin.section("/",-1) );
153     it->setWhatsThis(bin); //file to be saved/run
154     it->setToolTip(bin);
155     it->setCheckState(Qt::Checked);
156   ui->list_session_start->addItem(it);
157   ui->list_session_start->setCurrentItem(it);
158     settingChanged();
159 }
160