Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 22 | #include "logging.h" |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 23 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 24 | #include <QValidator> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 25 | |
| 26 | #include <boost/make_shared.hpp> |
| 27 | |
| 28 | using namespace boost; |
| 29 | using namespace Ccnx; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 31 | INIT_LOGGER ("Gui"); |
| 32 | |
| 33 | ChronoShareGui::ChronoShareGui(QWidget *parent) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 34 | : QDialog(parent) |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 35 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 36 | |
| 37 | setWindowTitle("Preferences"); |
| 38 | |
| 39 | labelUsername = new QLabel("Username"); |
| 40 | labelSharedFolder = new QLabel("Shared Folder Name"); |
| 41 | labelSharedFolderPath = new QLabel("Shared Folder Path"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 42 | |
| 43 | QRegExp regex("(^/[^/]+)+$"); |
| 44 | QValidator *prefixValidator = new QRegExpValidator(regex, this); |
| 45 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 46 | editUsername = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 47 | editUsername->setValidator(prefixValidator); |
| 48 | |
| 49 | QRegExp noPureWhiteSpace("^\\S+.*$"); |
| 50 | QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 51 | editSharedFolder = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 52 | editSharedFolder->setValidator(wsValidator); |
| 53 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 54 | editSharedFolderPath = new QLineEdit(); |
| 55 | editSharedFolderPath->setReadOnly(true); |
| 56 | QPalette pal = editSharedFolderPath->palette(); |
| 57 | pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base)); |
| 58 | editSharedFolderPath->setPalette(pal); |
| 59 | button = new QPushButton("Submit"); |
| 60 | label = new QLabel(); |
| 61 | |
| 62 | connect(button, SIGNAL(clicked()), this, SLOT(changeSettings())); |
| 63 | |
| 64 | mainLayout = new QVBoxLayout; //vertically |
| 65 | mainLayout->addWidget(labelUsername); |
| 66 | mainLayout->addWidget(editUsername); |
| 67 | mainLayout->addWidget(labelSharedFolder); |
| 68 | mainLayout->addWidget(editSharedFolder); |
| 69 | mainLayout->addWidget(labelSharedFolderPath); |
| 70 | mainLayout->addWidget(editSharedFolderPath); |
| 71 | mainLayout->addWidget(button); |
| 72 | mainLayout->addWidget(label); |
| 73 | setLayout(mainLayout); |
| 74 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 75 | // load settings |
| 76 | if(!loadSettings()) |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 78 | // prompt user to choose folder |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 79 | openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem."); |
| 80 | viewSettings(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 81 | openFileDialog(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 82 | viewSettings(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 83 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 85 | // create actions that result from clicking a menu option |
| 86 | createActions(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 88 | // create tray icon |
| 89 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 91 | // set icon image |
| 92 | setIcon(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 94 | // show tray icon |
| 95 | m_trayIcon->show(); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 96 | |
| 97 | // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, |
| 98 | // const std::string &sharedFolder, const boost::filesystem::path &rootDir, |
| 99 | // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2); |
| 100 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 101 | m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (), |
| 102 | m_dirPath.toStdString (), make_shared<CcnxWrapper> ()); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 103 | |
| 104 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 105 | m_watcher = new FsWatcher (m_dirPath, |
| 106 | bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1), |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 107 | bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1), |
| 108 | m_dispatcher->GetFileState ()); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | ChronoShareGui::~ChronoShareGui() |
| 112 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 113 | delete m_watcher; // stop filewatching ASAP |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 114 | delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object) |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 116 | // cleanup |
| 117 | delete m_trayIcon; |
| 118 | delete m_trayIconMenu; |
| 119 | delete m_openFolder; |
| 120 | delete m_viewSettings; |
| 121 | delete m_changeFolder; |
| 122 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 123 | |
| 124 | delete labelUsername; |
| 125 | delete labelSharedFolder; |
| 126 | delete editUsername; |
| 127 | delete editSharedFolder; |
| 128 | delete button; |
| 129 | delete label; |
| 130 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 134 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 135 | QMessageBox messageBox(this); |
| 136 | messageBox.setWindowTitle(title); |
| 137 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 139 | messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png")); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 141 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 145 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 146 | QMessageBox messageBox(this); |
| 147 | messageBox.setWindowTitle(title); |
| 148 | messageBox.setText(text); |
| 149 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 151 | messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png")); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 153 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void ChronoShareGui::createActions() |
| 157 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 158 | // create the "open folder" action |
| 159 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 160 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 162 | // create the "view settings" action |
| 163 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 164 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 166 | // create the "change folder" action |
| 167 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 168 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 170 | // create the "quit program" action |
| 171 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 172 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void ChronoShareGui::createTrayIcon() |
| 176 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 177 | // create a new icon menu |
| 178 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 179 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 180 | // add actions to the menu |
| 181 | m_trayIconMenu->addAction(m_openFolder); |
| 182 | m_trayIconMenu->addSeparator(); |
| 183 | m_trayIconMenu->addAction(m_viewSettings); |
| 184 | m_trayIconMenu->addAction(m_changeFolder); |
| 185 | m_trayIconMenu->addSeparator(); |
| 186 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 188 | // create new tray icon |
| 189 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 190 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 191 | // associate the menu with the tray icon |
| 192 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 194 | // handle left click of icon |
| 195 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void ChronoShareGui::setIcon() |
| 199 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 200 | // set the icon image |
| 201 | m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png")); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void ChronoShareGui::openSharedFolder() |
| 205 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 206 | // Alex: isn't there an OS-independent way in QT for this? |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 208 | // tell Finder to open the shared folder |
| 209 | QStringList scriptArgs; |
| 210 | scriptArgs << QLatin1String("-e") |
| 211 | << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"") |
| 212 | .arg(m_dirPath); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 214 | // execute the commands to make it happen |
| 215 | QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 217 | // clear command arguments |
| 218 | scriptArgs.clear(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 219 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 220 | // tell Finder to appear |
| 221 | scriptArgs << QLatin1String("-e") |
| 222 | << QLatin1String("tell application \"Finder\" to activate"); |
| 223 | |
| 224 | // execute the commands to make it happen |
| 225 | QProcess::execute("/usr/bin/osascript", scriptArgs); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 226 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 227 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 228 | void ChronoShareGui::changeSettings() |
| 229 | { |
| 230 | if(!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 231 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 232 | else |
| 233 | editUsername->setText(m_username); |
| 234 | |
| 235 | if(!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame^] | 236 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 237 | else |
| 238 | editSharedFolder->setText(m_sharedFolderName); |
| 239 | |
| 240 | saveSettings(); |
| 241 | this->hide(); |
| 242 | } |
| 243 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 244 | void ChronoShareGui::openFileDialog() |
| 245 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 246 | // prompt user for new directory |
| 247 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"), |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 248 | m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 249 | QFileInfo qFileInfo (tempPath); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 250 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 251 | if(qFileInfo.isDir()) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 252 | { |
| 253 | m_dirPath = tempPath; |
| 254 | editSharedFolderPath->setText(m_dirPath); |
| 255 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 256 | else |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 257 | { |
| 258 | openMessageBox ("Error", "Not a valid folder, Ignoring."); |
| 259 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 260 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 261 | _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ()); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 262 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 263 | // save settings |
| 264 | saveSettings(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 267 | void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 268 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 269 | // if double clicked, open shared folder |
| 270 | if(reason == QSystemTrayIcon::DoubleClick) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 271 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 272 | openSharedFolder(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 276 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 277 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 278 | //simple for now |
| 279 | this->show(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | bool ChronoShareGui::loadSettings() |
| 283 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 284 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 285 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 286 | // Load Settings |
| 287 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 288 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 289 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 290 | // _LOG_DEBUG (lexical_cast<string> (settings.allKeys())); |
| 291 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 292 | if(settings.contains("username")) |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 293 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 294 | m_username = settings.value("username", "admin").toString(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 295 | } |
| 296 | else |
| 297 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 298 | successful = false; |
| 299 | } |
| 300 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 301 | editUsername->setText(m_username); |
| 302 | |
| 303 | if(settings.contains("sharedfoldername")) |
| 304 | { |
| 305 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | successful = false; |
| 310 | } |
| 311 | |
| 312 | editSharedFolder->setText(m_sharedFolderName); |
| 313 | |
| 314 | if(settings.contains("dirPath")) |
| 315 | { |
| 316 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | successful = false; |
| 321 | } |
| 322 | |
| 323 | editSharedFolderPath->setText(m_dirPath); |
| 324 | |
| 325 | _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 326 | |
| 327 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void ChronoShareGui::saveSettings() |
| 331 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 332 | // Save Settings |
| 333 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 334 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 336 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 337 | settings.setValue("username", m_username); |
| 338 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 342 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 343 | _LOG_DEBUG ("Close Event") |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 344 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 345 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 346 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 347 | |
| 348 | #if WAF |
| 349 | #include "chronosharegui.moc" |
| 350 | #include "chronosharegui.cpp.moc" |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 351 | #endif |