Allow only one instance for useLocalServer option activated. If exists,

pass to it arguments.

For now, only allow to pass RsCollection file and retroshare://
protocol.
This commit is contained in:
Phenom 2016-03-01 13:08:33 +01:00
parent 9871b37ef2
commit 10bb542e83
19 changed files with 547 additions and 426 deletions

View file

@ -1,173 +0,0 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2006, 2007 crypton
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QCoreApplication>
#include <QMessageBox>
#include <QIcon>
#include <iostream>
#include <retroshare/rsinit.h>
#include "EventReceiver.h"
#include "gui/MainWindow.h"
#include "gui/RetroShareLink.h"
#ifdef WINDOWS_SYS
#include <windows.h>
#define OP_RETROSHARELINK 12000
#endif
struct SharedMemoryInfo
{
#ifdef WINDOWS_SYS
/* Store handle of the event window */
WId wid;
#else
long dummy;
#endif
};
EventReceiver::EventReceiver()
{
#ifdef WINDOWS_SYS
setWindowTitle("RetroShare EventReceiver");
#endif
/* Build unique name for the running instance */
QString name = QString("RetroShare-%1::EventReceiver").arg(QCoreApplication::applicationDirPath());
sharedMemory.setKey(name);
}
EventReceiver::~EventReceiver()
{
sharedMemory.detach();
}
bool EventReceiver::start()
{
if (!sharedMemory.create(sizeof(SharedMemoryInfo))) {
std::cerr << "EventReceiver::start() Cannot create shared memory !" << sharedMemory.errorString().toStdString() << std::endl;
return false;
}
bool result = true;
if (sharedMemory.lock()) {
SharedMemoryInfo *info = (SharedMemoryInfo*) sharedMemory.data();
if (info) {
#ifdef WINDOWS_SYS
info->wid = winId();
#else
info->dummy = 0;
#endif
} else {
result = false;
std::cerr << "EventReceiver::start() Shared memory returns a NULL pointer!" << std::endl;
}
sharedMemory.unlock();
} else {
result = false;
std::cerr << "EventReceiver::start() Cannot lock shared memory !" << std::endl;
}
return result;
}
bool EventReceiver::sendRetroShareLink(const QString& link)
{
if (!sharedMemory.attach()) {
/* No running instance found */
return false;
}
bool result = true;
if (sharedMemory.lock()) {
SharedMemoryInfo *info = (SharedMemoryInfo*) sharedMemory.data();
if (info) {
#ifdef WINDOWS_SYS
if (info->wid) {
QByteArray linkData(link.toUtf8());
COPYDATASTRUCT send;
send.dwData = OP_RETROSHARELINK;
send.cbData = link.length() * sizeof(char);
send.lpData = (void*) linkData.constData();
SendMessage((HWND) info->wid, WM_COPYDATA, (WPARAM) 0, (LPARAM) (PCOPYDATASTRUCT) &send);
} else {
result = false;
}
#else
Q_UNUSED(link);
QMessageBox mb(QMessageBox::Critical, "RetroShare", QObject::tr("Start with a RetroShare link is only supported for Windows."), QMessageBox::Ok);
mb.exec();
result = false;
#endif
} else {
result = false;
std::cerr << "EventReceiver::sendRetroShareLink() Cannot lock shared memory !" << std::endl;
}
sharedMemory.unlock();
} else {
result = false;
std::cerr << "EventReceiver::start() Cannot lock shared memory !" << std::endl;
}
sharedMemory.detach();
return result;
}
#ifdef WINDOWS_SYS
bool EventReceiver::winEvent(MSG* message, long* result)
{
if (message->message == WM_COPYDATA ) {
/* Extract the struct from lParam */
COPYDATASTRUCT *data = (COPYDATASTRUCT*) message->lParam;
if (data && data->dwData == OP_RETROSHARELINK) {
received(QString::fromUtf8((const char*) data->lpData, data->cbData));
/* Keep the event from Qt */
*result = 0;
return true;
}
}
/* Give the event to Qt */
return false;
}
#endif
void EventReceiver::received(const QString &url)
{
RetroShareLink link(url);
if (link.valid()) {
MainWindow::raiseWindow();
emit linkReceived(link.toUrl());
}
}

View file

@ -1,60 +0,0 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2006,2007 crypton
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _EVENTRECEIVER_H
#define _EVENTRECEIVER_H
#include <QWidget>
#include <QSharedMemory>
class QUrl;
class EventReceiver : public
#ifdef WINDOWS_SYS
QWidget
#else
QObject
#endif
{
Q_OBJECT
public:
EventReceiver();
~EventReceiver();
bool start();
bool sendRetroShareLink(const QString& link);
signals:
void linkReceived(const QUrl& url);
private:
void received(const QString& url);
#ifdef WINDOWS_SYS
/* Extend QWidget with a class that will capture the WM_COPYDATA messages */
bool winEvent (MSG* message, long* result);
#endif // WINDOWS_SYS
QSharedMemory sharedMemory;
};
#endif

View file

@ -1,7 +1,7 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2008, crypton
* This file is distributed under the following license:
*
* Copyright (c) 2008, crypton
* Copyright (c) 2008, Matt Edman, Justin Hipple
*
* This program is free software; you can redistribute it and/or
@ -28,6 +28,11 @@
#include "stringutil.h"
//#define CHAR_ARRAY_TO_STRINGLIST_DEBUG
#ifdef CHAR_ARRAY_TO_STRINGLIST_DEBUG
#include <iostream>
#include <QFile>
#endif
/** Create a QStringList from the array of C-style strings. */
QStringList
@ -35,7 +40,20 @@ char_array_to_stringlist(char **arr, int len)
{
QStringList list;
for (int i = 0; i < len; i++) {
list << QString(arr[i]);
#ifdef WINDOWS_SYS
list << QString::fromLatin1(arr[i]);
#else
list << QString::fromUtf8(arr[i]);
#endif
#ifdef CHAR_ARRAY_TO_STRINGLIST_DEBUG
std::cerr << "arr[" << i << "]==" << arr[i] << std::endl;
if (QFile(arr[i]).exists()) std::cerr << "arr[i] File exists" << std::endl;
std::cerr << "QString UTF8==" << QString::fromUtf8(arr[i]).toStdString() << std::endl;
if (QFile(QString::fromUtf8(arr[i])).exists()) std::cerr << "QString UTF8 File exists" << std::endl;
std::cerr << "QString Latin1==" << QString::fromLatin1(arr[i]).toStdString() << std::endl;
if (QFile(QString::fromLatin1(arr[i])).exists()) std::cerr << "QString Latin1 File exists" << std::endl;
#endif
}
return list;
}