blob: 9fca3b320136cd24261b8ee05bbd0f1d93f17553 [file] [log] [blame]
Jared Lindblom06405c42013-01-17 20:48:39 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Jared Lindblom <lindblom@cs.ucla.edu>
19 */
20
21#include "chronosharegui.h"
Alexander Afanasyev83a53002013-01-24 11:12:01 -080022#include "logging.h"
Jared Lindblom06405c42013-01-17 20:48:39 -080023
Alexander Afanasyev83a53002013-01-24 11:12:01 -080024INIT_LOGGER ("Gui");
25
26ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080027 : QDialog(parent)
Jared Lindblom06405c42013-01-17 20:48:39 -080028{
Jared Lindblomc8ffd592013-01-25 00:00:30 -080029
30 setWindowTitle("Preferences");
31
32 labelUsername = new QLabel("Username");
33 labelSharedFolder = new QLabel("Shared Folder Name");
34 labelSharedFolderPath = new QLabel("Shared Folder Path");
35 editUsername = new QLineEdit();
36 editSharedFolder = new QLineEdit();
37 editSharedFolderPath = new QLineEdit();
38 editSharedFolderPath->setReadOnly(true);
39 QPalette pal = editSharedFolderPath->palette();
40 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
41 editSharedFolderPath->setPalette(pal);
42 button = new QPushButton("Submit");
43 label = new QLabel();
44
45 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
46
47 mainLayout = new QVBoxLayout; //vertically
48 mainLayout->addWidget(labelUsername);
49 mainLayout->addWidget(editUsername);
50 mainLayout->addWidget(labelSharedFolder);
51 mainLayout->addWidget(editSharedFolder);
52 mainLayout->addWidget(labelSharedFolderPath);
53 mainLayout->addWidget(editSharedFolderPath);
54 mainLayout->addWidget(button);
55 mainLayout->addWidget(label);
56 setLayout(mainLayout);
57
Alexander Afanasyev83a53002013-01-24 11:12:01 -080058 // load settings
59 if(!loadSettings())
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080060 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -080061 // prompt user to choose folder
Jared Lindblomc8ffd592013-01-25 00:00:30 -080062 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
63 viewSettings();
Alexander Afanasyev83a53002013-01-24 11:12:01 -080064 openFileDialog();
Jared Lindblomc8ffd592013-01-25 00:00:30 -080065 viewSettings();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080066 }
Jared Lindblomb4032e22013-01-17 23:50:51 -080067
Alexander Afanasyev83a53002013-01-24 11:12:01 -080068 // create actions that result from clicking a menu option
69 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080070
Alexander Afanasyev83a53002013-01-24 11:12:01 -080071 // create tray icon
72 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080073
Alexander Afanasyev83a53002013-01-24 11:12:01 -080074 // set icon image
75 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -080076
Alexander Afanasyev83a53002013-01-24 11:12:01 -080077 // show tray icon
78 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080079
80 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
81 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
82 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
83
Jared Lindblomc8ffd592013-01-25 00:00:30 -080084
85 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080086 m_watcher = new FsWatcher (m_dirPath);
Jared Lindblom06405c42013-01-17 20:48:39 -080087}
88
89ChronoShareGui::~ChronoShareGui()
90{
Jared Lindblomc8ffd592013-01-25 00:00:30 -080091 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080092
Alexander Afanasyev83a53002013-01-24 11:12:01 -080093 // cleanup
94 delete m_trayIcon;
95 delete m_trayIconMenu;
96 delete m_openFolder;
97 delete m_viewSettings;
98 delete m_changeFolder;
99 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800100
101 delete labelUsername;
102 delete labelSharedFolder;
103 delete editUsername;
104 delete editSharedFolder;
105 delete button;
106 delete label;
107 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800108}
109
110void ChronoShareGui::openMessageBox(QString title, QString text)
111{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800112 QMessageBox messageBox(this);
113 messageBox.setWindowTitle(title);
114 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800115
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800116 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800117
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800118 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800119}
120
121void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
122{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800123 QMessageBox messageBox(this);
124 messageBox.setWindowTitle(title);
125 messageBox.setText(text);
126 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800127
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800128 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800129
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800130 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800131}
132
133void ChronoShareGui::createActions()
134{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800135 // create the "open folder" action
136 m_openFolder = new QAction(tr("&Open Folder"), this);
137 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800138
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800139 // create the "view settings" action
140 m_viewSettings = new QAction(tr("&View Settings"), this);
141 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800142
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800143 // create the "change folder" action
144 m_changeFolder = new QAction(tr("&Change Folder"), this);
145 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800146
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800147 // create the "quit program" action
148 m_quitProgram = new QAction(tr("&Quit"), this);
149 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800150}
151
152void ChronoShareGui::createTrayIcon()
153{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800154 // create a new icon menu
155 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800156
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800157 // add actions to the menu
158 m_trayIconMenu->addAction(m_openFolder);
159 m_trayIconMenu->addSeparator();
160 m_trayIconMenu->addAction(m_viewSettings);
161 m_trayIconMenu->addAction(m_changeFolder);
162 m_trayIconMenu->addSeparator();
163 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800164
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800165 // create new tray icon
166 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800167
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800168 // associate the menu with the tray icon
169 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800170
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800171 // handle left click of icon
172 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800173}
174
175void ChronoShareGui::setIcon()
176{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800177 // set the icon image
178 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
Jared Lindblom06405c42013-01-17 20:48:39 -0800179}
180
181void ChronoShareGui::openSharedFolder()
182{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800183 // Alex: isn't there an OS-independent way in QT for this?
Jared Lindblomb4032e22013-01-17 23:50:51 -0800184
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800185 // tell Finder to open the shared folder
186 QStringList scriptArgs;
187 scriptArgs << QLatin1String("-e")
188 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
189 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800190
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800191 // execute the commands to make it happen
192 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800193
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800194 // clear command arguments
195 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800196
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800197 // tell Finder to appear
198 scriptArgs << QLatin1String("-e")
199 << QLatin1String("tell application \"Finder\" to activate");
200
201 // execute the commands to make it happen
202 QProcess::execute("/usr/bin/osascript", scriptArgs);
Jared Lindblom06405c42013-01-17 20:48:39 -0800203}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800204
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800205void ChronoShareGui::changeSettings()
206{
207 if(!editUsername->text().isEmpty())
208 m_username = editUsername->text();
209 else
210 editUsername->setText(m_username);
211
212 if(!editSharedFolder->text().isEmpty())
213 m_sharedFolderName = editSharedFolder->text();
214 else
215 editSharedFolder->setText(m_sharedFolderName);
216
217 saveSettings();
218 this->hide();
219}
220
Jared Lindblomb4032e22013-01-17 23:50:51 -0800221void ChronoShareGui::openFileDialog()
222{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800223 // prompt user for new directory
224 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800225 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
226 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800227
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800228 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800229 {
230 m_dirPath = tempPath;
231 editSharedFolderPath->setText(m_dirPath);
232 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800233 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800234 {
235 openMessageBox ("Error", "Not a valid folder, Ignoring.");
236 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800237
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800238 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800239
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800240 // save settings
241 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800242}
243
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800244void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800245{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800246 // if double clicked, open shared folder
247 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800248 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800249 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800250 }
251}
252
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800253void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800254{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800255 //simple for now
256 this->show();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800257}
258
259bool ChronoShareGui::loadSettings()
260{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800261 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800262
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800263 // Load Settings
264 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
265 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800266
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800267 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
268
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800269 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800270 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800271 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 }
273 else
274 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800275 successful = false;
276 }
277
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800278 editUsername->setText(m_username);
279
280 if(settings.contains("sharedfoldername"))
281 {
282 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
283 }
284 else
285 {
286 successful = false;
287 }
288
289 editSharedFolder->setText(m_sharedFolderName);
290
291 if(settings.contains("dirPath"))
292 {
293 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
294 }
295 else
296 {
297 successful = false;
298 }
299
300 editSharedFolderPath->setText(m_dirPath);
301
302 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800303
304 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800305}
306
307void ChronoShareGui::saveSettings()
308{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800309 // Save Settings
310 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
311 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800312
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800313 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800314 settings.setValue("username", m_username);
315 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800316}
317
318void ChronoShareGui::closeEvent(QCloseEvent* event)
319{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800320 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800321 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800322 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800323}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800324
325#if WAF
326#include "chronosharegui.moc"
327#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800328#endif