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,11 +6,12 @@
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QUrl>
#include <QTextStream>
#include <QProcess>
ApplicationManager & ApplicationManager::get()
ApplicationManager &ApplicationManager::get()
{
static ApplicationManager manager;
return manager;
......@@ -18,10 +19,10 @@ ApplicationManager & ApplicationManager::get()
ApplicationManager::ApplicationManager()
{
const QString appsDir =
QString appsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/applications");
const QString iconsDir =
QString iconsDir =
QDir::cleanPath(QApplication::applicationDirPath() +
"/../share/icons/hicolor");
scanForApplications(appsDir, iconsDir);
......@@ -30,7 +31,7 @@ ApplicationManager::ApplicationManager()
QList<Application> ApplicationManager::applications() const
{
QList<Application> result;
foreach (const ApplicationPrivate & app_p, applications_) {
foreach (const ApplicationPrivate &app_p, applications_) {
Application app;
app.name = app_p.name;
app.icon = app_p.icon;
......@@ -41,13 +42,14 @@ QList<Application> ApplicationManager::applications() const
return result;
}
void ApplicationManager::scanForApplications(const QString &appsDirPath,
const QString &iconsDirPath)
{
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) {
foreach (const QString &fileName, files) {
QFile f(appsDir.absoluteFilePath(fileName));
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream ts(&f);
......@@ -58,27 +60,24 @@ void ApplicationManager::scanForApplications(const QString &appsDirPath,
app.mdiInterface = false;
bool isApp = false;
bool noDysplay = false;
foreach (const QString & line, lines) {
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();
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]") {
} else if (key.startsWith("name")) {
if (app.name.isEmpty() || key == "name[ru]") {
app.name = value;
}
}
else if (key == "icon") {
const QString iconFileName =
} else if (key == "icon") {
QString iconFileName =
iconsDir.absoluteFilePath(
QString("32x32/apps/%1.png").arg(value)
);
app.icon = QIcon(iconFileName);
}
else if (key == "exec") {
} else if (key == "exec") {
QString cleanName = value;
int spacePos = cleanName.indexOf(' ');
if (spacePos != -1) {
......@@ -92,13 +91,11 @@ void ApplicationManager::scanForApplications(const QString &appsDirPath,
#ifdef Q_OS_WIN32
app.executableFileName += ".exe";
#endif
}
else if (key == "x-kumir-mdi") {
} else if (key == "x-kumir-mdi") {
app.mdiInterface =
value.toLower() == "true" ||
value.toLower() == "1";
}
else if (key == "nodisplay") {
} else if (key == "nodisplay") {
noDysplay =
value.toLower() == "true" ||
value.toLower() == "1";
......@@ -114,34 +111,34 @@ void ApplicationManager::scanForApplications(const QString &appsDirPath,
void ApplicationManager::open(quintptr applicationId, const QUrl &url)
{
ApplicationPrivate * app = reinterpret_cast<ApplicationPrivate*>(applicationId);
const QString sUrl = url.toLocalFile();
ApplicationPrivate *app = reinterpret_cast<ApplicationPrivate *>(applicationId);
QString sUrl = url.toLocalFile();
bool startNewProcess = true;
Pid pid = 0;
if (app->mdiInterface) {
ProcessManager & procMan = ProcessManager::get();
ProcessManager &procMan = ProcessManager::get();
pid = procMan.find(app->executableFileName);
startNewProcess = pid == 0;
}
if (startNewProcess) {
const QString executable = app->executableFileName;
const QStringList arguments = QStringList() << sUrl;
QString executable = app->executableFileName;
QStringList arguments = QStringList() << sUrl;
QProcess::startDetached(executable, arguments);
}
else {
Messager & messager = Messager::get();
} else {
Messager &messager = Messager::get();
messager.sendMessage(pid, "OPEN " + sUrl);
}
Settings & sett = Settings::get();
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) {
Application result;
result.id = 0u;
foreach (const Application &app, apps) {
if (app.key == key) {
result = app;
break;
......
......@@ -4,7 +4,7 @@
#include <QtGlobal>
#include <QString>
#include <QIcon>
#include <QUrl>
class QUrl;
struct Application {
quintptr id;
......@@ -16,17 +16,19 @@ struct Application {
class ApplicationManager
{
public:
static ApplicationManager & get();
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);
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);
void scanForApplications(const QString &appsDir, const QString &iconsDir);
struct ApplicationPrivate {
QString name;
......
......@@ -2,17 +2,16 @@
#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);
const Settings & sett = Settings::get();
const Settings &sett = Settings::get();
int selectedIndex = 0;
foreach (const Application & app, apps) {
foreach (const Application &app, apps) {
if (app.key == sett.lastSelectedKey()) {
selectedIndex = ui_->comboBox->count();
}
......@@ -31,7 +30,8 @@ quintptr OpenDialog::selectedApplicationId() const
void OpenDialog::accept()
{
Settings & sett = Settings::get();
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
public:
explicit OpenDialog(const QList<Application> & apps);
explicit OpenDialog(const QList<Application> &apps);
quintptr selectedApplicationId() const;
void accept();
private:
Ui::OpenDialog * ui_;
Ui::OpenDialog *ui_;
};
......
......@@ -6,12 +6,12 @@
#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)
......@@ -20,28 +20,25 @@ void Worker::customEvent(QEvent *processEvent)
if (!QFileInfo(absPath).isAbsolute()) {
absPath = QDir::current().absoluteFilePath(absPath);
}
const QUrl url = QUrl::fromLocalFile(absPath);
QUrl url = QUrl::fromLocalFile(absPath);
ApplicationManager & appManager = ApplicationManager::get();
ApplicationManager &appManager = ApplicationManager::get();
if (appManager.applications().size() > 1) {
const Settings & sett = Settings::get();
const Settings &sett = Settings::get();
if (sett.skipOpenDialog() &&
appManager.find(sett.lastSelectedKey()).id)
{
appManager.find(sett.lastSelectedKey()).id) {
const Application app = appManager.find(sett.lastSelectedKey());
appManager.open(app, url);
}
else {
} else {
OpenDialog dialog(appManager.applications());
if (QDialog::Accepted == dialog.exec()) {
appManager.open(dialog.selectedApplicationId(), url);
}
}
}
else if (appManager.applications().size() == 1) {
} else if (appManager.applications().size() == 1) {
const quintptr id = appManager.applications().at(0).id;
appManager.open(id, url);
}
......@@ -49,11 +46,13 @@ void Worker::customEvent(QEvent *processEvent)
qApp->quit();
}
int main(int argc, char ** argv)
int main(int argc, char **argv)
{
if (argc < 2) { return 127; }
if (argc < 2) {
return 127;
}
QApplication qapp(argc, argv);
Worker * worker = new Worker;
Worker *worker = new Worker;
qapp.postEvent(worker, new QEvent(QEvent::User));
return qapp.exec();
}
......@@ -8,7 +8,7 @@
Messager::Messager() {}
Messager & Messager::get()
Messager &Messager::get()
{
static Messager messager;
if (!messager.pImpl_) {
......
......@@ -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,12 +17,13 @@ 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 {
class ImplInterface
{
public:
virtual void sendMessage(Pid receiver, const QString & message) = 0;
virtual void sendMessage(Pid receiver, const QString &message) = 0;
virtual ~ImplInterface() {}
};
......
#include "messager_shm.h"
#include <Windows.h>
#include <Psapi.h>
#include <QDebug>
......@@ -8,8 +7,10 @@
void MessagerShm::sendMessage(Pid pid, const QString &message)
{
const HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid);
HANDLE hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid
);
if (hProcess) {
wchar_t buffer[1024];
memset(buffer, 0, sizeof(buffer));
......@@ -28,3 +29,4 @@ void MessagerShm::sendMessage(Pid pid, const QString &message)
}
}
}
......@@ -3,11 +3,10 @@
#include "messager.h"
class MessagerShm
: public Messager::ImplInterface
class MessagerShm : public Messager::ImplInterface
{
public:
void sendMessage(Pid pid, const QString & message);
void sendMessage(Pid pid, const QString &message);
};
#endif
......@@ -29,24 +29,24 @@ serverinfo_t create_listen_socket()
memset(&addr, 0, sizeof(addr_len));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (0 != bind(sock, (struct sockaddr*) &addr, addr_len)) {
if (0 != bind(sock, (struct sockaddr *) &addr, addr_len)) {
exit(1);
}
if (0 != listen(sock, 1)) {
exit(1);
}
getsockname(sock, (struct sockaddr*) &addr, &addr_len);
getsockname(sock, (struct sockaddr *) &addr, &addr_len);
serverinfo_t result;
result.si_socket = sock;
result.si_port = ntohs(addr.sin_port);
return result;
}
static void serve_data(const char * data, size_t data_len, int socket)
static void serve_data(const char *data, size_t data_len, int socket)
{
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
int fd = accept(socket, (struct sockaddr*) &addr, &addr_len);
int fd = accept(socket, (struct sockaddr *) &addr, &addr_len);
if (-1 != fd) {
int status = send(fd, data, data_len, 0);
if (status == -1) {
......@@ -58,7 +58,7 @@ void MessagerUnix::sendMessage(pid_t receiver, const QString &message)
{
QByteArray utf8 = message.toUtf8();
size_t buffer_len = utf8.size() + 1;
char * buffer = (char*) malloc(buffer_len * sizeof(char));
char *buffer = (char *) malloc(buffer_len * sizeof(char));
strcpy(buffer, utf8.constData());
serverinfo_t si;
si = create_listen_socket();
......@@ -66,3 +66,4 @@ void MessagerUnix::sendMessage(pid_t receiver, const QString &message)
serve_data(buffer, buffer_len, si.si_socket);
free(buffer);
}
......@@ -3,8 +3,7 @@
#include "messager.h"
class MessagerUnix
: public Messager::ImplInterface
class MessagerUnix : public Messager::ImplInterface
{
public:
void sendMessage(pid_t receiver, const QString &message);
......
......@@ -9,7 +9,7 @@
ProcessManager::ProcessManager() {}
ProcessManager & ProcessManager::get()
ProcessManager &ProcessManager::get()
{
static ProcessManager manager;
if (manager.pImpl_ == 0) {
......@@ -27,8 +27,7 @@ Pid ProcessManager::find(const QString &executable)
{
if (pImpl_) {
return pImpl_->find(executable);
}
else {
} else {
return 0;
}
}
......@@ -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;
......@@ -15,10 +15,11 @@ typedef pid_t Pid;
class ProcessManager
{
public:
static ProcessManager & get();
Pid find(const QString & executable);
static ProcessManager &get();
Pid find(const QString &executable);
class ImplInterface {
class ImplInterface
{
public:
virtual Pid find(const QString &executable) = 0;
virtual ~ImplInterface() {}
......
......@@ -11,7 +11,7 @@ pid_t ProcManagerLinux::find(const QString &executable)
foreach (const QFileInfo procDirInfo, procs) {
QFileInfo procExeFile(procDirInfo.absoluteFilePath() + "/exe");
if (procExeFile.exists() && procExeFile.isReadable() && procExeFile.isSymLink()) {
const QString target = procExeFile.symLinkTarget();
QString target = procExeFile.symLinkTarget();
if (target == executable) {
result = procDirInfo.fileName().toULongLong();
break;
......
......@@ -3,8 +3,7 @@
#include "procmanager.h"
class ProcManagerLinux
: public ProcessManager::ImplInterface
class ProcManagerLinux : public ProcessManager::ImplInterface
{
public:
pid_t find(const QString &executable);
......
......@@ -14,22 +14,24 @@ Pid ProcManagerWindows::find(const QString &executable)
static const DWORD cb = sizeof(processIds);
DWORD bytesReturned;
const QString key = QFileInfo(executable).fileName();
QString key = QFileInfo(executable).fileName();
EnumProcesses(processIds, cb, &bytesReturned);
const size_t numberOfProcesses = bytesReturned / sizeof(DWORD);
size_t numberOfProcesses = bytesReturned / sizeof(DWORD);
for (size_t i=0u; i<numberOfProcesses; i++) {
const DWORD pid = processIds[i];
const HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid);
for (size_t i = 0u; i < numberOfProcesses; i++) {
DWORD pid = processIds[i];
HANDLE hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid
);
if (hProcess) {
wchar_t buffer[1024];
memset(buffer, 0, sizeof(buffer));
const DWORD bufSize = GetModuleBaseNameW(hProcess, 0, buffer, 1024u);
DWORD bufSize = GetModuleBaseNameW(hProcess, 0, buffer, 1024u);
CloseHandle(hProcess);
const QString procName = QString::fromWCharArray(buffer, bufSize);
QString procName = QString::fromWCharArray(buffer, bufSize);
if (procName == key) {
result = pid;
break;
......
......@@ -3,8 +3,7 @@
#include "procmanager.h"
class ProcManagerWindows
: public ProcessManager::ImplInterface
class ProcManagerWindows : public ProcessManager::ImplInterface
{
public:
Pid find(const QString &executable);
......
#include "settings.h"
Settings & Settings::get()
Settings &Settings::get()
{
QSettings::setDefaultFormat(QSettings::IniFormat);
static Settings settings("kumir2", "kumir2-open");
return settings;
}
Settings::Settings(const QString & dir, const QString & file)
: q_(dir, file)
Settings::Settings(const QString &dir, const QString &file) : q_(dir, file)
{
q_.setIniCodec("UTF-8");
}
......@@ -33,3 +32,4 @@ void Settings::setLastSelectedKey(const QString &key)
{
q_.setValue("LastSelected", key);
}
......@@ -2,21 +2,20 @@
#define SETTINGS_H
#include <QSettings>
#include <QVariant>
class Settings
{
public:
static Settings & get();
static Settings &get();
bool skipOpenDialog() const;
void setSkipOpenDialog(bool value);
QString lastSelectedKey() const;
void setLastSelectedKey(const QString & key);
void setLastSelectedKey(const QString &key);
private:
explicit Settings(const QString & dir, const QString & file);
explicit Settings(const QString &dir, const QString &file);
QSettings q_;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment