Merge pull request #297 from PhenomRetroShare/Add_AllowOnlyOneIntanceAndPassArg

Allow only one instance. If exists, pass to it arguments.
This commit is contained in:
Cyril Soler 2016-04-07 00:09:00 -04:00
commit c8cae4c33c
19 changed files with 547 additions and 426 deletions

View file

@ -203,6 +203,7 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags)
}
setWindowTitle(tr("RetroShare %1 a secure decentralized communication platform").arg(Rshare::retroshareVersion(true)) + " - " + nameAndLocation);
connect(rApp, SIGNAL(newArgsReceived(QStringList)), this, SLOT(receiveNewArgs(QStringList)));
/* add url handler for RetroShare links */
QDesktopServices::setUrlHandler(RSLINK_SCHEME, this, "retroshareLinkActivated");
@ -1095,6 +1096,12 @@ void MainWindow::doQuit()
rApp->quit();
}
void MainWindow::receiveNewArgs(QStringList args)
{
Rshare::parseArguments(args, false);
processLastArgs();
}
void MainWindow::displayErrorMessage(int /*a*/,int /*b*/,const QString& error_msg)
{
QMessageBox::critical(NULL, tr("Internal Error"),error_msg) ;
@ -1433,6 +1440,33 @@ void MainWindow::retroshareLinkActivated(const QUrl &url)
RetroShareLink::process(links);
}
void MainWindow::openRsCollection(const QString &filename)
{
QFileInfo qinfo(filename);
if (qinfo.exists()) {
if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString)) {
RsCollectionFile collection;
collection.openColl(qinfo.absoluteFilePath());
}
}
}
void MainWindow::processLastArgs()
{
while (!Rshare::links()->isEmpty()) {
std::cerr << "MainWindow::processLastArgs() : " << Rshare::links()->count() << std::endl;
/* Now use links from the command line, because no RetroShare was running */
RetroShareLink link(Rshare::links()->takeFirst());
if (link.valid()) {
retroshareLinkActivated(link.toUrl());
}
}
while (!Rshare::files()->isEmpty()) {
/* Now use files from the command line, because no RetroShare was running */
openRsCollection(Rshare::files()->takeFirst());
}
}
//void MainWindow::servicePermission()
//{
// ServicePermissionDialog::showYourself();

View file

@ -173,13 +173,16 @@ public:
SysTrayStatus *sysTrayStatusInstance();
public slots:
void receiveNewArgs(QStringList args);
void displayErrorMessage(int,int,const QString&) ;
void postModDirectories(bool update_local);
void displayDiskSpaceWarning(int loc,int size_limit_mb) ;
void checkAndSetIdle(int idleTime);
void retroshareLinkActivated(const QUrl &url);
void externalLinkActivated(const QUrl &url);
void retroshareLinkActivated(const QUrl &url);
void openRsCollection(const QString &filename);
void processLastArgs();
//! Go to a specific part of the control panel.
void setNewPage(int page);
void setCompactStatusMode(bool compact);

View file

@ -49,18 +49,31 @@ GeneralPage::GeneralPage(QWidget * parent, Qt::WindowFlags flags)
ui.chkRunRetroshareAtSystemStartupMinimized->setEnabled(false);
#endif
if (Settings->canSetRetroShareProtocol() == false) {
ui.enableRetroShareProtocol->setEnabled(false);
} else {
ui.adminLabel->setEnabled(false);
ui.adminLabel->setToolTip("");
}
#else
ui.chkRunRetroshareAtSystemStartup->setVisible(false);
ui.chkRunRetroshareAtSystemStartupMinimized->setVisible(false);
ui.enableRetroShareProtocol->setVisible(false);
ui.registerRetroShareProtocol->setVisible(false);
ui.adminLabel->setVisible(false);
#endif
if (Settings->canSetRetroShareProtocol()) {
ui.registerRetroShareProtocol->setEnabled(true);
#ifdef Q_OS_WIN
ui.adminLabel->setEnabled(true);
ui.adminLabel->setToolTip(tr("You have enough right."));
#else
ui.desktopFileMissingLabel->setVisible(false);
#endif
} else {
ui.registerRetroShareProtocol->setEnabled(false);
#ifdef Q_OS_WIN
ui.adminLabel->setEnabled(false);
ui.adminLabel->setToolTip(tr("You don't have enough right. Run RetroShare as Admin to change this setting."));
#else
ui.desktopFileMissingLabel->setVisible(true);
#endif
}
ui.useLocalServer->setEnabled(true);
}
/** Destructor */
@ -75,42 +88,44 @@ void GeneralPage::runStartWizard()
/** Saves the changes on this page */
bool GeneralPage::save(QString &/*errmsg*/)
{
Settings->setStartMinimized(ui.checkStartMinimized->isChecked());
Settings->setValue("doQuit", ui.checkQuit->isChecked());
Settings->setCloseToTray(ui.checkCloseToTray->isChecked());
#ifdef Q_OS_WIN
#ifndef QT_DEBUG
Settings->setRunRetroshareOnBoot(ui.chkRunRetroshareAtSystemStartup->isChecked(), ui.chkRunRetroshareAtSystemStartupMinimized->isChecked());
#endif
if (ui.enableRetroShareProtocol->isChecked() != Settings->getRetroShareProtocol()) {
if (Settings->setRetroShareProtocol(ui.enableRetroShareProtocol->isChecked()) == false) {
if (ui.enableRetroShareProtocol->isChecked()) {
QMessageBox::critical(this, tr("Error"), tr("Could not add retroshare:// as protocol."));
} else {
QMessageBox::critical(this, tr("Error"), tr("Could not remove retroshare:// protocol."));
}
}
}
#endif
Settings->setMaxTimeBeforeIdle(ui.spinBox->value());
RsInit::setAutoLogin(ui.autoLogin->isChecked());
Settings->setStartMinimized(ui.checkStartMinimized->isChecked());
if (ui.checkAdvanced->isChecked())
{
std::string opt("YES");
rsConfig->setConfigurationOption(RS_CONFIG_ADVANCED, opt);
std::string opt("YES");
rsConfig->setConfigurationOption(RS_CONFIG_ADVANCED, opt);
}
else
{
std::string opt("NO");
rsConfig->setConfigurationOption(RS_CONFIG_ADVANCED, opt);
std::string opt("NO");
rsConfig->setConfigurationOption(RS_CONFIG_ADVANCED, opt);
}
Settings->setValue("doQuit", ui.checkQuit->isChecked());
Settings->setCloseToTray(ui.checkCloseToTray->isChecked());
RsInit::setAutoLogin(ui.autoLogin->isChecked());
if (ui.registerRetroShareProtocol->isChecked() != Settings->getRetroShareProtocol()) {
QString error ="";
if (Settings->setRetroShareProtocol(ui.registerRetroShareProtocol->isChecked(), error) == false) {
if (ui.registerRetroShareProtocol->isChecked()) {
QMessageBox::critical(this, tr("Error"), tr("Could not add retroshare:// as protocol.").append("\n").append(error));
} else {
QMessageBox::critical(this, tr("Error"), tr("Could not remove retroshare:// protocol.").append("\n").append(error));
}
}
}
Settings->setUseLocalServer(ui.useLocalServer->isChecked());
Settings->setMaxTimeBeforeIdle(ui.idleSpinBox->value());
return true;
}
@ -121,25 +136,23 @@ void GeneralPage::load()
bool minimized;
ui.chkRunRetroshareAtSystemStartup->setChecked(Settings->runRetroshareOnBoot(minimized));
ui.chkRunRetroshareAtSystemStartupMinimized->setChecked(minimized);
ui.enableRetroShareProtocol->setChecked(Settings->getRetroShareProtocol());
#endif
ui.checkStartMinimized->setChecked(Settings->getStartMinimized());
ui.checkQuit->setChecked(Settings->value("doQuit", false).toBool());
ui.checkCloseToTray->setChecked(Settings->getCloseToTray());
ui.spinBox->setValue(Settings->getMaxTimeBeforeIdle());
ui.autoLogin->setChecked(RsInit::getAutoLogin());
bool advancedmode = false;
std::string advsetting;
if (rsConfig->getConfigurationOption(RS_CONFIG_ADVANCED, advsetting) && (advsetting == "YES"))
{
advancedmode = true;
advancedmode = true;
}
ui.checkAdvanced->setChecked(advancedmode);
ui.checkQuit->setChecked(Settings->value("doQuit", false).toBool());
ui.checkCloseToTray->setChecked(Settings->getCloseToTray());
ui.autoLogin->setChecked(RsInit::getAutoLogin());
ui.registerRetroShareProtocol->setChecked(Settings->getRetroShareProtocol());
ui.useLocalServer->setChecked(Settings->getUseLocalServer());
ui.idleSpinBox->setValue(Settings->getMaxTimeBeforeIdle());
}

View file

@ -10,13 +10,13 @@
<height>465</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout">
<item>
<widget class="QGroupBox" name="groupBox_2">
<widget class="QGroupBox" name="startupGBox">
<property name="title">
<string>Startup</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<layout class="QGridLayout" name="startupGBoxGLayout">
<property name="verticalSpacing">
<number>2</number>
</property>
@ -35,9 +35,9 @@
</widget>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout" name="runRetroshareAtSystemStartupMinimizedHLayout">
<item>
<spacer name="horizontalSpacer">
<spacer name="runRetroshareAtSystemStartupMinimizedHSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
@ -65,11 +65,11 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_4">
<widget class="QGroupBox" name="advGBox">
<property name="title">
<string>For Advanced Users </string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QVBoxLayout" name="advGBoxVLayout">
<item>
<widget class="QCheckBox" name="checkAdvanced">
<property name="text">
@ -81,11 +81,11 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<widget class="QGroupBox" name="miscGBox">
<property name="title">
<string>Misc</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<layout class="QGridLayout" name="miscGBoxGLayout">
<property name="verticalSpacing">
<number>2</number>
</property>
@ -111,9 +111,9 @@
</widget>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<layout class="QHBoxLayout" name="retroshareProtocolHLayout">
<item>
<widget class="QCheckBox" name="enableRetroShareProtocol">
<widget class="QCheckBox" name="registerRetroShareProtocol">
<property name="minimumSize">
<size>
<width>0</width>
@ -121,7 +121,7 @@
</size>
</property>
<property name="text">
<string>Register retroshare:// as URL protocol (Restart required)</string>
<string>Register retroshare:// as URL protocol</string>
</property>
</widget>
</item>
@ -143,24 +143,53 @@
</item>
</layout>
</item>
<item row="5" column="0">
<widget class="QCheckBox" name="useLocalServer">
<property name="toolTip">
<string>When checked, this instance receives new parameters (like RsLink or RsFile) and avoid new one.</string>
</property>
<property name="text">
<string>Use Local Server to get new arguments.</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="desktopFileMissingLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Install RetroShare with a package installer to get&lt;/p&gt;&lt;p&gt;/usr/share/applications/retroshare06.desktop&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>!!!The RetroShare's desktop file is missing or wrong!!!</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<widget class="QGroupBox" name="idleGBox">
<property name="title">
<string>Idle</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="idleGBoxHLayout">
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="idleLabel">
<property name="text">
<string>Idle Time</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox">
<widget class="QSpinBox" name="idleSpinBox">
<property name="suffix">
<string> seconds</string>
</property>
@ -176,7 +205,7 @@
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<spacer name="mainVSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>

View file

@ -741,31 +741,47 @@ RshareSettings::setRunRetroshareOnBoot(bool run, bool minimized)
#endif
}
#if defined(Q_OS_WIN)
static QString getAppPathForProtocol()
{
#if defined(Q_OS_WIN)
return "\"" + QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + "\" -r \"%1\"";
}
#elif defined(Q_OS_LINUX)
return QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " %U";
#endif
}
/** Returns true if retroshare:// is registered as protocol */
bool RshareSettings::getRetroShareProtocol()
{
#if defined(Q_OS_WIN)
/* Check key */
QSettings retroshare("HKEY_CLASSES_ROOT\\retroshare", QSettings::NativeFormat);
QSettings retroshare("HKEY_CURRENT_USER\\Software\\Classes\\retroshare", QSettings::NativeFormat);
if (retroshare.contains("Default")) {
/* Check profile */
if (retroshare.value("Profile").toString().toStdString() == rsPeers->getOwnId().toStdString()) {
/* URL Protocol */
if (retroshare.contains("URL Protocol")) {
/* Check app path */
QSettings command("HKEY_CLASSES_ROOT\\retroshare\\shell\\open\\command", QSettings::NativeFormat);
QSettings command("HKEY_CURRENT_USER\\Software\\Classes\\retroshare\\shell\\open\\command", QSettings::NativeFormat);
if (command.value("Default").toString() == getAppPathForProtocol()) {
return true;
}
}
}
#elif defined(Q_OS_LINUX)
QFile desktop("/usr/share/applications/retroshare06.desktop");
if (desktop.exists()) {
desktop.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&desktop);
QStringList lines;
while(!in.atEnd()) {
lines << in.readLine();
}
desktop.close();
if (lines.contains("Exec=" + getAppPathForProtocol()))
if (lines.contains("MimeType=x-scheme-handler/retroshare"))
return true;
}
#else
/* Platforms other than windows aren't supported yet */
/* Platforms not supported yet */
#endif
return false;
}
@ -774,50 +790,72 @@ bool RshareSettings::getRetroShareProtocol()
bool RshareSettings::canSetRetroShareProtocol()
{
#if defined(Q_OS_WIN)
QSettings retroshare("HKEY_CLASSES_ROOT\\retroshare", QSettings::NativeFormat);
return retroshare.isWritable();
QSettings classRoot("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
return classRoot.isWritable();
#elif defined(Q_OS_LINUX)
return RshareSettings::getRetroShareProtocol();
#else
return false;
#endif
}
/** Register retroshare:// as protocol */
bool RshareSettings::setRetroShareProtocol(bool value)
bool RshareSettings::setRetroShareProtocol(bool value, QString &error)
{
error = "";
#if defined(Q_OS_WIN)
if (value) {
QSettings retroshare("HKEY_CLASSES_ROOT\\retroshare", QSettings::NativeFormat);
QSettings retroshare("HKEY_CURRENT_USER\\Software\\Classes\\retroshare", QSettings::NativeFormat);
retroshare.setValue("Default", "URL: RetroShare protocol");
QSettings::Status state = retroshare.status();
if (state == QSettings::AccessError) {
error = tr("Registry Access Error. Maybe you need Administrator right.");
return false;
}
retroshare.setValue("URL Protocol", "");
retroshare.setValue("Profile", QString::fromStdString(rsPeers->getOwnId().toStdString()));
QSettings command("HKEY_CLASSES_ROOT\\retroshare\\shell\\open\\command", QSettings::NativeFormat);
QSettings command("HKEY_CURRENT_USER\\Software\\Classes\\retroshare\\shell\\open\\command", QSettings::NativeFormat);
command.setValue("Default", getAppPathForProtocol());
state = command.status();
} else {
QSettings retroshare("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
retroshare.remove("retroshare");
QSettings classRoot("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
classRoot.remove("retroshare");
QSettings::Status state = retroshare.status();
QSettings::Status state = classRoot.status();
if (state == QSettings::AccessError) {
error = tr("Registry Access Error. Maybe you need Administrator right.");
return false;
}
}
return true;
#else
/* Platforms other than windows aren't supported yet */
#elif defined(Q_OS_LINUX)
/* RetroShare protocol is always activated for Linux */
Q_UNUSED(value);
return true;
#else
/* Platforms not supported yet */
Q_UNUSED(value);
return false;
#endif
}
/** Returns true if this instance have to run Local Server*/
bool RshareSettings::getUseLocalServer()
{
return value("UseLocalServer", true).toBool();
}
/** Sets whether to run Local Server */
void RshareSettings::setUseLocalServer(bool value)
{
if (value != getUseLocalServer()) {
setValue("UseLocalServer", value);
Rshare::updateLocalServer();
}
}
/** Saving Generic Widget Size / Location */
void RshareSettings::saveWidgetInformation(QWidget *widget)

View file

@ -155,8 +155,13 @@ public:
bool canSetRetroShareProtocol();
/** Returns true if retroshare:// is registered as protocol */
bool getRetroShareProtocol();
/** Register retroshare:// as protocl */
bool setRetroShareProtocol(bool value);
/** Register retroshare:// as protocol */
bool setRetroShareProtocol(bool value, QString &error);
/** Returns true if this instance have to run Local Server*/
bool getUseLocalServer();
/** Sets whether to run Local Server */
void setUseLocalServer(bool value);
/* Get the destination log file. */
QString getLogFile();