Commit cf00cd75 authored by Anupam Basak's avatar Anupam Basak

Initial Functionality

parents
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.out
*.app
############
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
build
CMakeLists.txt.user
*.pro.user
.qmake.stash
project(maui-accounts-dbus-daemon)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FeatureSummary)
find_package(ECM 1.7.0 REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${ECM_MODULE_PATH})
find_package(
Qt5 REQUIRED NO_MODULE
COMPONENTS
Core
DBus
)
find_package(
KF5 REQUIRED
COMPONENTS
Wallet
)
set(
SOURCES
src/main.cpp
src/dbus-interfaces/RootDBusInterface.cpp
)
set(
HEADERS
src/dbus-interfaces/DBusInterface.hpp
src/dbus-interfaces/RootDBusInterface.hpp
)
add_executable(
${PROJECT_NAME}
${SOURCES}
${HEADERS}
)
target_link_libraries(
${PROJECT_NAME}
Qt5::Core
Qt5::DBus
KF5::Wallet
)
feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
#ifndef DBUS_INTERFACES_DBUSINTERFACE_HPP
#define DBUS_INTERFACES_DBUSINTERFACE_HPP
#include <QObject>
class DBusInterface : public QObject {
Q_OBJECT
public:
virtual QString name() = 0;
};
#endif
#include "RootDBusInterface.hpp"
#include <QDebug>
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QUuid>
QString RootDBusInterface::name() { return "org.mauikit.accounts"; }
RootDBusInterface::RootDBusInterface() {
QDir appDataFolder(QStandardPaths::writableLocation(
QStandardPaths::StandardLocation::AppDataLocation));
if (!appDataFolder.exists()) {
appDataFolder.mkpath(".");
}
accountsJsonFilePath =
QStandardPaths::writableLocation(
QStandardPaths::StandardLocation::AppDataLocation) +
"/accounts.json";
wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0,
KWallet::Wallet::OpenType::Synchronous);
if (!wallet->hasFolder(WALLET_FOLDER_NAME)) {
wallet->createFolder(WALLET_FOLDER_NAME);
}
wallet->setFolder(WALLET_FOLDER_NAME);
QFile accountJsonFile(accountsJsonFilePath);
if (!accountJsonFile.open(QIODevice::ReadWrite)) {
qWarning("Couldn't open config file.");
}
accountsJsonObject =
QJsonDocument::fromJson(accountJsonFile.readAll()).object();
accountJsonFile.close();
}
void RootDBusInterface::writeAccountsJsonObjectToFile() {
QFile accountJsonFile(accountsJsonFilePath);
if (!accountJsonFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning("Couldn't open config file.");
}
accountJsonFile.write(QJsonDocument(accountsJsonObject).toJson());
accountJsonFile.close();
}
QList<QVariant> RootDBusInterface::getAccountNames() {
qDebug() << "getAccountNames :";
QList<QVariant> list;
QJsonArray accounts = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QList<QString> accountsStringArray;
for (int i = 0; i < accounts.size(); i++) {
QJsonObject account = accounts[i].toObject();
list.append(account[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME].toString());
}
return list;
}
QMap<QString, QVariant> RootDBusInterface::getAccount(QString id) {
qDebug() << "getAccount :" << id;
QMap<QString, QVariant> returnVal;
QJsonArray accounts = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QList<QString> accountsStringArray;
for (int i = 0; i < accounts.size(); i++) {
QJsonObject account = accounts[i].toObject();
if (account[JSON_ACCOUNT_ARRAY_FIELD_ID] == id) {
QByteArray password;
wallet->readEntry(account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString(),
password);
returnVal.insert(
"name", account[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME].toString());
returnVal.insert("username",
account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString());
returnVal.insert("password",
QString::fromStdString(password.toStdString()));
returnVal.insert("extras",
account[JSON_ACCOUNT_ARRAY_FIELD_EXTRAS].toString());
}
}
return returnVal;
}
QString RootDBusInterface::createAccount(QString name, QString username,
QString password, QString extras) {
qDebug() << "createAccount :" << name << username << password << extras;
if (!accountsJsonObject.contains(JSON_FIELD_ACCOUNTS)) {
accountsJsonObject[JSON_FIELD_ACCOUNTS] = QJsonArray();
}
QString id = QUuid::createUuid().toString(QUuid::StringFormat::WithoutBraces);
QJsonArray accountsArray = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QJsonObject accountObject;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_EXTRAS] = extras;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME] = username;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME] = name;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_ID] = id;
wallet->writeEntry(username,
QByteArray::fromStdString(password.toStdString()));
accountsArray.append(accountObject);
accountsJsonObject[JSON_FIELD_ACCOUNTS] = accountsArray;
writeAccountsJsonObjectToFile();
return id;
}
bool RootDBusInterface::removeAccount(QString id) {
qDebug() << "removeAccount :" << id;
bool accountDeleted = false;
QJsonArray accountsArray = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
for (int i = 0; i < accountsArray.size(); i++) {
QJsonObject accountObject = accountsArray[i].toObject();
if (accountObject[JSON_ACCOUNT_ARRAY_FIELD_ID].toString() == id) {
qDebug() << "Removing account" << id;
accountsArray.removeAt(i);
accountsJsonObject[JSON_FIELD_ACCOUNTS] = accountsArray;
wallet->removeEntry(
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString());
accountDeleted = true;
break;
}
}
writeAccountsJsonObjectToFile();
return accountDeleted;
}
#ifndef DBUS_INTERFACES_ROOTDBUSINTERFACE_HPP
#define DBUS_INTERFACES_ROOTDBUSINTERFACE_HPP
#include "DBusInterface.hpp"
#include <KWallet>
#include <QJsonObject>
#include <QList>
#include <QString>
#include <QVariant>
class RootDBusInterface : public DBusInterface {
Q_OBJECT
public:
QString name() override;
RootDBusInterface();
private:
KWallet::Wallet *wallet;
QJsonObject accountsJsonObject;
const QString WALLET_FOLDER_NAME = "org.mauikit.accounts";
const QString WALLET_ENTRY_ACCOUNTS = "accounts";
const QString JSON_FIELD_ACCOUNTS = "accounts";
const QString JSON_ACCOUNT_ARRAY_FIELD_EXTRAS = "extras";
const QString JSON_ACCOUNT_ARRAY_FIELD_USERNAME = "username";
const QString JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME = "account_name";
const QString JSON_ACCOUNT_ARRAY_FIELD_ID = "_id";
QString accountsJsonFilePath;
void writeAccountsJsonObjectToFile();
public slots:
Q_SCRIPTABLE QList<QVariant> getAccountNames();
Q_SCRIPTABLE QMap<QString, QVariant> getAccount(QString id);
Q_SCRIPTABLE QString createAccount(QString name, QString username,
QString password, QString extras);
Q_SCRIPTABLE bool removeAccount(QString id);
};
#endif
#include "dbus-interfaces/DBusInterface.hpp"
#include "dbus-interfaces/RootDBusInterface.hpp"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusError>
#define SERVICE_NAME "org.mauikit.accounts"
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
fprintf(stderr,
"Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
if (!QDBusConnection::sessionBus().registerService(SERVICE_NAME)) {
fprintf(stderr, "%s\n",
qPrintable(QDBusConnection::sessionBus().lastError().message()));
exit(1);
}
RootDBusInterface *interface = new RootDBusInterface();
QDBusConnection::sessionBus().registerObject(
"/", interface->name(), interface, QDBusConnection::ExportAllSlots);
return app.exec();
}
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