Added persistent settings and file dialog to change folder
diff --git a/gui/ChronoShareGui.pro.user b/gui/ChronoShareGui.pro.user
index 9e10f46..30d20df 100644
--- a/gui/ChronoShareGui.pro.user
+++ b/gui/ChronoShareGui.pro.user
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
-<!-- Written by Qt Creator 2.6.0, 2013-01-17T19:13:59. -->
+<!-- Written by Qt Creator 2.6.0, 2013-01-17T23:46:43. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -54,7 +54,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QByteArray" key="ProjectExplorer.ProjectConfiguration.Id">{ec4ff4f3-f12d-4e42-870d-b8c64ad3c1a3}</value>
- <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
+ <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
diff --git a/gui/chronosharegui.cpp b/gui/chronosharegui.cpp
index 0bf70d8..0285f78 100644
--- a/gui/chronosharegui.cpp
+++ b/gui/chronosharegui.cpp
@@ -21,60 +21,140 @@
#include "chronosharegui.h"
#include "ui_chronosharegui.h"
-ChronoShareGui::ChronoShareGui(QString dirPath, QWidget *parent) :
+ChronoShareGui::ChronoShareGui(QWidget *parent) :
QWidget(parent),
- m_dirPath(dirPath)
+ m_fileDialogWidget(new QWidget())
{
+ // load settings
+ loadSettings();
+
+ // create actions that result from clicking a menu option
createActions();
+
+ // create tray icon
createTrayIcon();
+
+ // set icon image
setIcon();
+ // show tray icon
m_trayIcon->show();
}
ChronoShareGui::~ChronoShareGui()
{
+ // cleanup
delete m_trayIcon;
delete m_trayIconMenu;
delete m_openFolder;
+ delete m_changeFolder;
delete m_quitProgram;
+ delete m_fileDialogWidget;
}
void ChronoShareGui::createActions()
{
- m_openFolder = new QAction(tr("&Open"), this);
+ // create the "open folder" action
+ m_openFolder = new QAction(tr("&Open Folder"), this);
connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
+ // create the "change folder" action
+ m_changeFolder = new QAction(tr("&Change Folder"), this);
+ connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
+
+ // create the "quit program" action
m_quitProgram = new QAction(tr("&Quit"), this);
connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
+
}
void ChronoShareGui::createTrayIcon()
{
+ // create a new icon menu
m_trayIconMenu = new QMenu(this);
+ // add actions to the menu
m_trayIconMenu->addAction(m_openFolder);
+ m_trayIconMenu->addAction(m_changeFolder);
m_trayIconMenu->addSeparator();
m_trayIconMenu->addAction(m_quitProgram);
+ // create new tray icon
m_trayIcon = new QSystemTrayIcon(this);
+
+ // associate the menu with the tray icon
m_trayIcon->setContextMenu(m_trayIconMenu);
+
+ // handle left click of icon
+ connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
}
void ChronoShareGui::setIcon()
{
+ // set the icon image
m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
}
void ChronoShareGui::openSharedFolder()
{
+ // tell Finder to open the shared folder
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
.arg(m_dirPath);
+
+ // execute the commands to make it happen
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
+
+ // clear command arguments
scriptArgs.clear();
+
+ // tell Finder to appear
scriptArgs << QLatin1String("-e")
<< QLatin1String("tell application \"Finder\" to activate");
+
+ // execute the commands to make it happen
QProcess::execute("/usr/bin/osascript", scriptArgs);
}
+
+void ChronoShareGui::openFileDialog()
+{
+ // prompt user for new directory
+ m_dirPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
+ m_dirPath, QFileDialog::ShowDirsOnly |
+ QFileDialog::DontResolveSymlinks);
+
+ qDebug() << m_dirPath;
+
+ // save settings
+ saveSettings();
+}
+
+void ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
+{
+ // if double clicked, open shared folder
+ if(reason == QSystemTrayIcon::DoubleClick)
+ {
+ openSharedFolder();
+ }
+}
+
+void ChronoShareGui::loadSettings()
+{
+ // Load Settings
+ QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
+ m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
+}
+
+void ChronoShareGui::saveSettings()
+{
+ // Save Settings
+ QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
+ settings.setValue("dirPath", m_dirPath);
+}
+
+void ChronoShareGui::closeEvent(QCloseEvent* event)
+{
+ qDebug() << "Close Event.";
+ event->ignore(); // don't let the event propagate to the base class
+}
diff --git a/gui/chronosharegui.h b/gui/chronosharegui.h
index 6e26734..c1ae9b2 100644
--- a/gui/chronosharegui.h
+++ b/gui/chronosharegui.h
@@ -24,9 +24,12 @@
#include <QWidget>
#include <QSystemTrayIcon>
#include <QMenu>
-#include <QCloseEvent>
#include <QDebug>
#include <QProcess>
+#include <QSettings>
+#include <QDir>
+#include <QFileDialog>
+#include <QCloseEvent>
class ChronoShareGui : public QWidget
{
@@ -34,12 +37,18 @@
public:
// constructor
- ChronoShareGui(QString dirPath, QWidget* parent = 0);
+ explicit ChronoShareGui(QWidget* parent = 0);
// destructor
~ChronoShareGui();
-
+
private slots:
+ // open the shared folder
+ void openSharedFolder();
+
+ // open file dialog
+ void openFileDialog();
+
// handle left click of tray icon
void trayIconClicked(QSystemTrayIcon::ActivationReason reason);
@@ -53,17 +62,28 @@
// set icon image
void setIcon();
- // open the shared folder
- void openSharedFolder();
+ // load persistent settings
+ void loadSettings();
+
+ // save persistent settings
+ void saveSettings();
+
+ // capture close event
+ void closeEvent(QCloseEvent* event);
private:
QSystemTrayIcon* m_trayIcon; // tray icon
QMenu* m_trayIconMenu; // tray icon menu
QAction* m_openFolder; // open the shared folder action
+ QAction* m_changeFolder; // change the shared folder action
QAction* m_quitProgram; // quit program action
QString m_dirPath; // shared directory
+
+ QString m_settingsFilePath; // settings file path
+
+ QWidget* m_fileDialogWidget; // file dialog widget
};
#endif // CHRONOSHAREGUI_H
diff --git a/gui/main.cpp b/gui/main.cpp
index 7387182..0b9a176 100644
--- a/gui/main.cpp
+++ b/gui/main.cpp
@@ -23,10 +23,13 @@
int main(int argc, char *argv[])
{
- QApplication a(argc, argv);
+ QApplication app(argc, argv);
- // invoke instance of gui for this shared directory
- ChronoShareGui w("/Users/jared/Desktop");
+ // do not quit when last window closes
+ app.setQuitOnLastWindowClosed(false);
+
+ // invoke gui
+ ChronoShareGui gui;
- return a.exec();
+ return app.exec();
}