Commit 44b3f493 authored by Alexander A. Maly's avatar Alexander A. Maly

Astyled tools/open

Trying to not include windows.h in headers.
parent a1ea7c6b
Pipeline #2674 failed with stages
in 2 minutes and 6 seconds
......@@ -6,146 +6,143 @@
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QUrl>
#include <QTextStream>
#include <QProcess>
ApplicationManager & ApplicationManager::get()
ApplicationManager &ApplicationManager::get()
{
static ApplicationManager manager;
return manager;
static ApplicationManager manager;
return manager;
}
ApplicationManager::ApplicationManager()
{
const QString appsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/applications");
const QString iconsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/icons/hicolor");
scanForApplications(appsDir, iconsDir);
QString appsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/applications");
QString iconsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/icons/hicolor");
scanForApplications(appsDir, iconsDir);
}
QList<Application> ApplicationManager::applications() const
{
QList<Application> result;
foreach (const ApplicationPrivate & app_p, applications_) {
Application app;
app.name = app_p.name;
app.icon = app_p.icon;
app.id = quintptr(&app_p);
app.key = QFileInfo(app_p.executableFileName).baseName();
result.push_back(app);
}
return result;
QList<Application> result;
foreach (const ApplicationPrivate &app_p, applications_) {
Application app;
app.name = app_p.name;
app.icon = app_p.icon;
app.id = quintptr(&app_p);
app.key = QFileInfo(app_p.executableFileName).baseName();
result.push_back(app);
}
return result;
}
void ApplicationManager::scanForApplications(const QString &appsDirPath,
const QString &iconsDirPath)
{
QDir appsDir(appsDirPath);
QDir iconsDir(iconsDirPath);
QStringList files = appsDir.entryList(QStringList() << "kumir2-*.desktop");
foreach (const QString & fileName, files) {
QFile f(appsDir.absoluteFilePath(fileName));
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream ts(&f);
ts.setCodec("UTF-8");
QStringList lines = ts.readAll().split("\n");
f.close();
ApplicationPrivate app;
app.mdiInterface = false;
bool isApp = false;
bool noDysplay = false;
foreach (const QString & line, lines) {
int eqPos = line.indexOf('=');
if (eqPos != -1) {
const QString key = line.left(eqPos).trimmed().toLower();
const QString value = line.mid(eqPos + 1).trimmed();
if (key == "type") {
isApp = value.toLower() == "application";
}
else if (key.startsWith("name")) {
if (app.name.isEmpty() || key=="name[ru]") {
app.name = value;
}
}
else if (key == "icon") {
const QString iconFileName =
iconsDir.absoluteFilePath(
QString("32x32/apps/%1.png").arg(value)
);
app.icon = QIcon(iconFileName);
}
else if (key == "exec") {
QString cleanName = value;
int spacePos = cleanName.indexOf(' ');
if (spacePos != -1) {
cleanName = cleanName.left(spacePos);
}
app.executableFileName =
QDir::cleanPath(
QApplication::applicationDirPath() +
"/" + cleanName
);
void ApplicationManager::scanForApplications(
const QString &appsDirPath,
const QString &iconsDirPath
) {
QDir appsDir(appsDirPath);
QDir iconsDir(iconsDirPath);
QStringList files = appsDir.entryList(QStringList() << "kumir2-*.desktop");
foreach (const QString &fileName, files) {
QFile f(appsDir.absoluteFilePath(fileName));
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream ts(&f);
ts.setCodec("UTF-8");
QStringList lines = ts.readAll().split("\n");
f.close();
ApplicationPrivate app;
app.mdiInterface = false;
bool isApp = false;
bool noDysplay = false;
foreach (const QString &line, lines) {
int eqPos = line.indexOf('=');
if (eqPos != -1) {
QString key = line.left(eqPos).trimmed().toLower();
QString value = line.mid(eqPos + 1).trimmed();
if (key == "type") {
isApp = value.toLower() == "application";
} else if (key.startsWith("name")) {
if (app.name.isEmpty() || key == "name[ru]") {
app.name = value;
}
} else if (key == "icon") {
QString iconFileName =
iconsDir.absoluteFilePath(
QString("32x32/apps/%1.png").arg(value)
);
app.icon = QIcon(iconFileName);
} else if (key == "exec") {
QString cleanName = value;
int spacePos = cleanName.indexOf(' ');
if (spacePos != -1) {
cleanName = cleanName.left(spacePos);
}
app.executableFileName =
QDir::cleanPath(
QApplication::applicationDirPath() +
"/" + cleanName
);
#ifdef Q_OS_WIN32
app.executableFileName += ".exe";
app.executableFileName += ".exe";
#endif
}
else if (key == "x-kumir-mdi") {
app.mdiInterface =
value.toLower() == "true" ||
value.toLower() == "1";
}
else if (key == "nodisplay") {
noDysplay =
value.toLower() == "true" ||
value.toLower() == "1";
}
}
}
if (isApp && !noDysplay) {
applications_.push_back(app);
}
}
}
} else if (key == "x-kumir-mdi") {
app.mdiInterface =
value.toLower() == "true" ||
value.toLower() == "1";
} else if (key == "nodisplay") {
noDysplay =
value.toLower() == "true" ||
value.toLower() == "1";
}
}
}
if (isApp && !noDysplay) {
applications_.push_back(app);
}
}
}
}
void ApplicationManager::open(quintptr applicationId, const QUrl &url)
{
ApplicationPrivate * app = reinterpret_cast<ApplicationPrivate*>(applicationId);
const QString sUrl = url.toLocalFile();
bool startNewProcess = true;
Pid pid = 0;
if (app->mdiInterface) {
ProcessManager & procMan = ProcessManager::get();
pid = procMan.find(app->executableFileName);
startNewProcess = pid == 0;
}
if (startNewProcess) {
const QString executable = app->executableFileName;
const QStringList arguments = QStringList() << sUrl;
QProcess::startDetached(executable, arguments);
}
else {
Messager & messager = Messager::get();
messager.sendMessage(pid, "OPEN " + sUrl);
}
ApplicationPrivate *app = reinterpret_cast<ApplicationPrivate *>(applicationId);
QString sUrl = url.toLocalFile();
bool startNewProcess = true;
Pid pid = 0;
if (app->mdiInterface) {
ProcessManager &procMan = ProcessManager::get();
pid = procMan.find(app->executableFileName);
startNewProcess = pid == 0;
}
if (startNewProcess) {
QString executable = app->executableFileName;
QStringList arguments = QStringList() << sUrl;
QProcess::startDetached(executable, arguments);
} else {
Messager &messager = Messager::get();
messager.sendMessage(pid, "OPEN " + sUrl);
}
Settings & sett = Settings::get();
sett.setLastSelectedKey(QFileInfo(app->executableFileName).baseName());
Settings &sett = Settings::get();
sett.setLastSelectedKey(QFileInfo(app->executableFileName).baseName());
}
Application ApplicationManager::find(const QString &key) const
{
QList<Application> apps = applications();
Application result; result.id = 0u;
foreach (const Application & app, apps) {
if (app.key == key) {
result = app;
break;
}
}
return result;
QList<Application> apps = applications();
Application result;
result.id = 0u;
foreach (const Application &app, apps) {
if (app.key == key) {
result = app;
break;
}
}
return result;
}
......@@ -4,37 +4,39 @@
#include <QtGlobal>
#include <QString>
#include <QIcon>
#include <QUrl>
class QUrl;
struct Application {
quintptr id;
QString key;
QString name;
QIcon icon;
quintptr id;
QString key;
QString name;
QIcon icon;
};
class ApplicationManager
{
public:
static ApplicationManager & get();
QList<Application> applications() const;
inline void open(const Application & application,
const QUrl & url) { open(application.id, url); }
void open(quintptr applicationId, const QUrl & url);
static ApplicationManager &get();
QList<Application> applications() const;
void open(const Application &application, const QUrl &url)
{
open(application.id, url);
}
void open(quintptr applicationId, const QUrl &url);
Application find(const QString & key) const;
Application find(const QString &key) const;
private:
explicit ApplicationManager();
void scanForApplications(const QString & appsDir, const QString & iconsDir);
struct ApplicationPrivate {
QString name;
QString executableFileName;
QIcon icon;
bool mdiInterface;
};
QList<ApplicationPrivate> applications_;
explicit ApplicationManager();
void scanForApplications(const QString &appsDir, const QString &iconsDir);
struct ApplicationPrivate {
QString name;
QString executableFileName;
QIcon icon;
bool mdiInterface;
};
QList<ApplicationPrivate> applications_;
};
......
......@@ -2,36 +2,36 @@
#include "ui_dialog.h"
#include "settings.h"
OpenDialog::OpenDialog(const QList<Application> & apps)
: QDialog(0)
, ui_(new Ui::OpenDialog)
OpenDialog::OpenDialog(const QList<Application> &apps) :
QDialog(0), ui_(new Ui::OpenDialog)
{
ui_->setupUi(this);
ui_->setupUi(this);
const Settings & sett = Settings::get();
const Settings &sett = Settings::get();
int selectedIndex = 0;
int selectedIndex = 0;
foreach (const Application & app, apps) {
if (app.key == sett.lastSelectedKey()) {
selectedIndex = ui_->comboBox->count();
}
ui_->comboBox->addItem(app.icon, app.name, app.id);
}
foreach (const Application &app, apps) {
if (app.key == sett.lastSelectedKey()) {
selectedIndex = ui_->comboBox->count();
}
ui_->comboBox->addItem(app.icon, app.name, app.id);
}
ui_->comboBox->setCurrentIndex(selectedIndex);
ui_->checkBox->setChecked(sett.skipOpenDialog());
ui_->hint->setVisible(ui_->checkBox->isChecked());
ui_->comboBox->setCurrentIndex(selectedIndex);
ui_->checkBox->setChecked(sett.skipOpenDialog());
ui_->hint->setVisible(ui_->checkBox->isChecked());
}
quintptr OpenDialog::selectedApplicationId() const
{
return ui_->comboBox->itemData(ui_->comboBox->currentIndex()).toULongLong();
return ui_->comboBox->itemData(ui_->comboBox->currentIndex()).toULongLong();
}
void OpenDialog::accept()
{
Settings & sett = Settings::get();
sett.setSkipOpenDialog(ui_->checkBox->isChecked());
QDialog::accept();
Settings &sett = Settings::get();
sett.setSkipOpenDialog(ui_->checkBox->isChecked());
QDialog::accept();
}
......@@ -4,20 +4,20 @@
#include <QDialog>
#include "appmanager.h"
namespace Ui {
namespace Ui
{
class OpenDialog;
}
class OpenDialog
: public QDialog
class OpenDialog : public QDialog
{
Q_OBJECT
Q_OBJECT
public:
explicit OpenDialog(const QList<Application> & apps);
quintptr selectedApplicationId() const;
void accept();
explicit OpenDialog(const QList<Application> &apps);
quintptr selectedApplicationId() const;
void accept();
private:
Ui::OpenDialog * ui_;
Ui::OpenDialog *ui_;
};
......
......@@ -6,54 +6,53 @@
#include <QObject>
#include <QFileInfo>
#include <QDir>
#include <QUrl>
class Worker
: public QObject
class Worker : public QObject
{
private:
void customEvent(QEvent * processEvent);
void customEvent(QEvent *processEvent);
};
void Worker::customEvent(QEvent *processEvent)
{
QString absPath = qApp->arguments().at(1);
if (!QFileInfo(absPath).isAbsolute()) {
absPath = QDir::current().absoluteFilePath(absPath);
}
const QUrl url = QUrl::fromLocalFile(absPath);
ApplicationManager & appManager = ApplicationManager::get();
if (appManager.applications().size() > 1) {
const Settings & sett = Settings::get();
if (sett.skipOpenDialog() &&
appManager.find(sett.lastSelectedKey()).id)
{
const Application app = appManager.find(sett.lastSelectedKey());
appManager.open(app, url);
}
else {
OpenDialog dialog(appManager.applications());
if (QDialog::Accepted == dialog.exec()) {
appManager.open(dialog.selectedApplicationId(), url);
}
}
}
else if (appManager.applications().size() == 1) {
const quintptr id = appManager.applications().at(0).id;
appManager.open(id, url);
}
processEvent->accept();
qApp->quit();
QString absPath = qApp->arguments().at(1);
if (!QFileInfo(absPath).isAbsolute()) {
absPath = QDir::current().absoluteFilePath(absPath);
}
QUrl url = QUrl::fromLocalFile(absPath);
ApplicationManager &appManager = ApplicationManager::get();
if (appManager.applications().size() > 1) {
const Settings &sett = Settings::get();
if (sett.skipOpenDialog() &&
appManager.find(sett.lastSelectedKey()).id) {
const Application app = appManager.find(sett.lastSelectedKey());
appManager.open(app, url);
} else {
OpenDialog dialog(appManager.applications());
if (QDialog::Accepted == dialog.exec()) {
appManager.open(dialog.selectedApplicationId(), url);
}
}
} else if (appManager.applications().size() == 1) {
const quintptr id = appManager.applications().at(0).id;
appManager.open(id, url);
}
processEvent->accept();
qApp->quit();
}
int main(int argc, char ** argv)
int main(int argc, char **argv)
{
if (argc < 2) { return 127; }
QApplication qapp(argc, argv);
Worker * worker = new Worker;
qapp.postEvent(worker, new QEvent(QEvent::User));
return qapp.exec();
if (argc < 2) {
return 127;
}
QApplication qapp(argc, argv);
Worker *worker = new Worker;
qapp.postEvent(worker, new QEvent(QEvent::User));
return qapp.exec();
}
......@@ -8,22 +8,22 @@
Messager::Messager() {}
Messager & Messager::get()
Messager &Messager::get()
{
static Messager messager;
if (!messager.pImpl_) {
static Messager messager;
if (!messager.pImpl_) {
#ifndef Q_OS_WIN32
messager.pImpl_.reset(new MessagerUnix);
messager.pImpl_.reset(new MessagerUnix);
#else
messager.pImpl_.reset(new MessagerShm);
messager.pImpl_.reset(new MessagerShm);
#endif
}
return messager;
}
return messager;
}
void Messager::sendMessage(Pid receiver, const QString &message)
{
if (pImpl_) {
pImpl_->sendMessage(receiver, message);
}
if (pImpl_) {
pImpl_->sendMessage(receiver, message);
}
}
......@@ -3,8 +3,8 @@
#include <QtGlobal>
#ifdef Q_OS_WIN32
#include <Windows.h>
typedef DWORD Pid;
//#include <Windows.h>
typedef quint32 Pid;
#else
#include <sys/types.h>
typedef pid_t Pid;
......@@ -17,18 +17,19 @@ class Messager
{
public:
static Messager & get();
void sendMessage(Pid receiver, const QString & message);
static Messager &get();
void sendMessage(Pid receiver, const QString &message);
class ImplInterface {
public:
virtual void sendMessage(Pid receiver, const QString & message) = 0;
virtual ~ImplInterface() {}
};
class ImplInterface
{
public:
virtual void sendMessage(Pid receiver, const QString &message) = 0;
virtual ~ImplInterface() {}
};
private:
explicit Messager();
QScopedPointer<ImplInterface> pImpl_;
explicit Messager();
QScopedPointer<ImplInterface> pImpl_;
};
#endif
#include "messager_shm.h"
#include <Windows.h>
#include <Psapi.h>
#include <QDebug>
......@@ -8,23 +7,26 @@
void MessagerShm::sendMessage(Pid pid, const QString &message)
{
const HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid);
if (hProcess) {
wchar_t buffer[1024];
memset(buffer, 0, sizeof(buffer));
DWORD sz = GetModuleBaseNameW(hProcess, 0, buffer, 1024u);
CloseHandle(hProcess);
QString procName = QString::fromWCharArray(buffer, sz);
if (procName.length() > 0) {
QSharedMemory shm(procName);
if (shm.attach(QSharedMemory::ReadWrite)) {
shm.lock();
QByteArray utf8 = message.toUtf8();
memset(shm.data(), 0, shm.size());
memcpy(shm.data(), utf8.constData(), utf8.size());
shm.unlock();
}
}
}
HANDLE hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid
);
if (hProcess) {
wchar_t buffer[1024];
memset(buffer, 0, sizeof(buffer));
DWORD sz = GetModuleBaseNameW(hProcess, 0, buffer, 1024u);
CloseHandle(hProcess);
QString procName = QString::fromWCharArray(buffer, sz);
if (procName.length() > 0) {
QSharedMemory shm(procName);
if (shm.attach(QSharedMemory::ReadWrite)) {
shm.lock();