From f01bf10dc511268ead551a191a6a3211c006ba44 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Sun, 24 Feb 2013 18:22:35 +0100 Subject: Implement Keyring system base --- libsettings/include/keyring.h | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 libsettings/include/keyring.h (limited to 'libsettings/include/keyring.h') diff --git a/libsettings/include/keyring.h b/libsettings/include/keyring.h new file mode 100644 index 00000000..4563a268 --- /dev/null +++ b/libsettings/include/keyring.h @@ -0,0 +1,85 @@ +/* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KEYRING_H +#define KEYRING_H + +#include + +#include "libsettings_config.h" + +/** + * @file libsettings/include/keyring.h + * Access to System Keyrings + */ + +/** + * @brief The Keyring class + * the System Keyring/Keychain/Wallet/Vault/etc + */ +class LIBMMCSETTINGS_EXPORT Keyring : public QObject +{ + Q_OBJECT +public: + /** + * @brief the System Keyring instance + * @return the Keyring instance + */ + static Keyring *instance(); + + /** + * @brief store a password in the Keyring + * @param service the service name + * @param username the account name + * @param password the password to store + * @return success + */ + virtual bool storePassword(QString service, QString username, QString password) = 0; + + /** + * @brief get a password from the Keyring + * @param service the service name + * @param username the account name + * @return the password (success=!isNull()) + */ + virtual QString getPassword(QString service, QString username) = 0; + + /** + * @brief lookup a password + * @param service the service name + * @param username the account name + * @return wether the password is available + */ + virtual bool hasPassword(QString service, QString username) = 0; + + /** + * @brief get a list of all stored accounts. + * @param service the service name + * @return + */ + virtual QStringList getStoredAccounts(QString service) = 0; + +protected: + /// fall back to StubKeyring if false + virtual bool isValid() { return false; } + +private: + static Keyring *m_instance; + static void destroy(); +}; + +#endif // KEYRING_H -- cgit From f4c9cb8c1d395422b7e4f1c27ac92b6df08a39bb Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Mon, 25 Feb 2013 22:47:03 +0100 Subject: refactor indendation, fix a bug in MinecraftProcess & fix a bug in InstanceLauncher --- java/constants.h | 8 +-- java/javautils.cpp | 86 ++++++++++++++++---------------- java/javautils.h | 4 +- libmultimc/include/minecraftprocess.h | 2 +- libmultimc/src/minecraftprocess.cpp | 2 + libsettings/include/keyring.h | 78 ++++++++++++++--------------- libsettings/src/keyring.cpp | 30 +++++------ libsettings/src/stubkeyring.cpp | 62 +++++++++++------------ libsettings/src/stubkeyring.h | 20 ++++---- libutil/include/cmdutils.h | 17 +++---- libutil/src/cmdutils.cpp | 16 +++--- main.cpp | 7 +-- test.cpp | 94 +++++++++++++++++------------------ 13 files changed, 211 insertions(+), 215 deletions(-) (limited to 'libsettings/include/keyring.h') diff --git a/java/constants.h b/java/constants.h index 80697ebe..61aa5687 100644 --- a/java/constants.h +++ b/java/constants.h @@ -7,7 +7,7 @@ namespace java class constant { public: - enum type_t : uint8_t + enum type_t : uint8_t { j_hole = 0, // HACK: this is a hole in the array, because java is crazy j_string_data = 1, @@ -145,7 +145,7 @@ namespace java uint16_t descriptor_index; } name_and_type; }; - }; + }; /** * A helper class that represents the custom container used in Java class file for storage of constants @@ -185,7 +185,7 @@ namespace java index++; } } - } + } typedef std::vector container_type; /** * Access constants based on jar file index numbers (index of the first element is 1) @@ -208,5 +208,5 @@ namespace java } private: container_type constants; - }; + }; } diff --git a/java/javautils.cpp b/java/javautils.cpp index bf4b5cdf..4a359031 100644 --- a/java/javautils.cpp +++ b/java/javautils.cpp @@ -26,56 +26,56 @@ namespace javautils QString GetMinecraftJarVersion(QString jarName) { - QString version = MCVer_Unknown; + QString version = MCVer_Unknown; - // check if minecraft.jar exists - QFile jar(jarName); - if (!jar.exists()) - return version; + // check if minecraft.jar exists + QFile jar(jarName); + if (!jar.exists()) + return version; - // open minecraft.jar - QuaZip zip(&jar); - if (!zip.open(QuaZip::mdUnzip)) - return version; + // open minecraft.jar + QuaZip zip(&jar); + if (!zip.open(QuaZip::mdUnzip)) + return version; - // open Minecraft.class - zip.setCurrentFile("net/minecraft/client/Minecraft.class", QuaZip::csSensitive); - QuaZipFile Minecraft(&zip); - if (!Minecraft.open(QuaZipFile::ReadOnly)) - return version; + // open Minecraft.class + zip.setCurrentFile("net/minecraft/client/Minecraft.class", QuaZip::csSensitive); + QuaZipFile Minecraft(&zip); + if (!Minecraft.open(QuaZipFile::ReadOnly)) + return version; - // read Minecraft.class - qint64 size = Minecraft.size(); - char *classfile = new char[size]; - Minecraft.read(classfile, size); + // read Minecraft.class + qint64 size = Minecraft.size(); + char *classfile = new char[size]; + Minecraft.read(classfile, size); - // parse Minecraft.class - try { - char *temp = classfile; - java::classfile MinecraftClass(temp, size); - java::constant_pool constants = MinecraftClass.constants; - for(java::constant_pool::container_type::const_iterator iter=constants.begin(); - iter != constants.end(); iter++) - { - const java::constant & constant = *iter; - if (constant.type != java::constant::j_string_data) - continue; - const std::string & str = constant.str_data; - if (str.compare(0, 20, "Minecraft Minecraft ") == 0) - { - version = str.substr(20).data(); - break; - } - } - } catch(java::classfile_exception &) {} + // parse Minecraft.class + try { + char *temp = classfile; + java::classfile MinecraftClass(temp, size); + java::constant_pool constants = MinecraftClass.constants; + for(java::constant_pool::container_type::const_iterator iter=constants.begin(); + iter != constants.end(); iter++) + { + const java::constant & constant = *iter; + if (constant.type != java::constant::j_string_data) + continue; + const std::string & str = constant.str_data; + if (str.compare(0, 20, "Minecraft Minecraft ") == 0) + { + version = str.substr(20).data(); + break; + } + } + } catch(java::classfile_exception &) {} - // clean up - delete[] classfile; - Minecraft.close(); - zip.close(); - jar.close(); + // clean up + delete[] classfile; + Minecraft.close(); + zip.close(); + jar.close(); - return version; + return version; } } diff --git a/java/javautils.h b/java/javautils.h index 7f9b6d89..883eff1d 100644 --- a/java/javautils.h +++ b/java/javautils.h @@ -21,8 +21,8 @@ namespace javautils { - /** - * @brief Get the version from a minecraft.jar by parsing its class files. Expensive! + /** + * @brief Get the version from a minecraft.jar by parsing its class files. Expensive! */ QString GetMinecraftJarVersion(QString jar); } diff --git a/libmultimc/include/minecraftprocess.h b/libmultimc/include/minecraftprocess.h index 8986f7ad..d6b9f612 100644 --- a/libmultimc/include/minecraftprocess.h +++ b/libmultimc/include/minecraftprocess.h @@ -85,7 +85,7 @@ protected: QStringList m_arguments; void genArgs(); - void log(QString text); + void log(QString text, ConsoleWindow::WriteMode mode = ConsoleWindow::MULTIMC); protected slots: void finish(int, QProcess::ExitStatus status); diff --git a/libmultimc/src/minecraftprocess.cpp b/libmultimc/src/minecraftprocess.cpp index 943d76b1..3aecb1ee 100644 --- a/libmultimc/src/minecraftprocess.cpp +++ b/libmultimc/src/minecraftprocess.cpp @@ -197,6 +197,8 @@ void MinecraftProcess::launch() start(m_instance->settings().get("JavaPath").toString(), m_arguments); if (!waitForStarted()) { + log("Could not launch minecraft!", ConsoleWindow::ERROR); + return; //TODO: error handling } diff --git a/libsettings/include/keyring.h b/libsettings/include/keyring.h index 4563a268..afdc3bba 100644 --- a/libsettings/include/keyring.h +++ b/libsettings/include/keyring.h @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -33,53 +33,53 @@ */ class LIBMMCSETTINGS_EXPORT Keyring : public QObject { - Q_OBJECT + Q_OBJECT public: - /** - * @brief the System Keyring instance - * @return the Keyring instance - */ - static Keyring *instance(); + /** + * @brief the System Keyring instance + * @return the Keyring instance + */ + static Keyring *instance(); - /** - * @brief store a password in the Keyring - * @param service the service name - * @param username the account name - * @param password the password to store - * @return success - */ - virtual bool storePassword(QString service, QString username, QString password) = 0; + /** + * @brief store a password in the Keyring + * @param service the service name + * @param username the account name + * @param password the password to store + * @return success + */ + virtual bool storePassword(QString service, QString username, QString password) = 0; - /** - * @brief get a password from the Keyring - * @param service the service name - * @param username the account name - * @return the password (success=!isNull()) - */ - virtual QString getPassword(QString service, QString username) = 0; + /** + * @brief get a password from the Keyring + * @param service the service name + * @param username the account name + * @return the password (success=!isNull()) + */ + virtual QString getPassword(QString service, QString username) = 0; - /** - * @brief lookup a password - * @param service the service name - * @param username the account name - * @return wether the password is available - */ - virtual bool hasPassword(QString service, QString username) = 0; + /** + * @brief lookup a password + * @param service the service name + * @param username the account name + * @return wether the password is available + */ + virtual bool hasPassword(QString service, QString username) = 0; - /** - * @brief get a list of all stored accounts. - * @param service the service name - * @return - */ - virtual QStringList getStoredAccounts(QString service) = 0; + /** + * @brief get a list of all stored accounts. + * @param service the service name + * @return + */ + virtual QStringList getStoredAccounts(QString service) = 0; protected: - /// fall back to StubKeyring if false - virtual bool isValid() { return false; } + /// fall back to StubKeyring if false + virtual bool isValid() { return false; } private: - static Keyring *m_instance; - static void destroy(); + static Keyring *m_instance; + static void destroy(); }; #endif // KEYRING_H diff --git a/libsettings/src/keyring.cpp b/libsettings/src/keyring.cpp index 1b13e35c..9eaba684 100644 --- a/libsettings/src/keyring.cpp +++ b/libsettings/src/keyring.cpp @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -37,27 +37,27 @@ class Win32Keystore; Keyring *Keyring::instance() { - if (m_instance == nullptr) - { + if (m_instance == nullptr) + { #ifdef KEYRING - m_instance = new KEYRING(); - if (!m_instance->isValid()) - { - qWarning("Could not create SystemKeyring! falling back to StubKeyring."); - m_instance = new StubKeyring(); - } + m_instance = new KEYRING(); + if (!m_instance->isValid()) + { + qWarning("Could not create SystemKeyring! falling back to StubKeyring."); + m_instance = new StubKeyring(); + } #else - qWarning("Keyrings are not supported on your OS. Fallback StubKeyring is insecure!"); - m_instance = new StubKeyring(); + qWarning("Keyrings are not supported on your OS. Fallback StubKeyring is insecure!"); + m_instance = new StubKeyring(); #endif - atexit(Keyring::destroy); - } - return m_instance; + atexit(Keyring::destroy); + } + return m_instance; } void Keyring::destroy() { - delete m_instance; + delete m_instance; } Keyring *Keyring::m_instance; diff --git a/libsettings/src/stubkeyring.cpp b/libsettings/src/stubkeyring.cpp index 0e29d2f2..963f3dd9 100644 --- a/libsettings/src/stubkeyring.cpp +++ b/libsettings/src/stubkeyring.cpp @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -25,72 +25,72 @@ int scrambler = 0x9586309; QString scramble(QString in_) { - QByteArray in = in_.toUtf8(); - QByteArray out; - for (int i = 0; itype = OptionType::Switch; + param->type = otSwitch; param->name = name; param->metavar = QString("<%1>").arg(name); param->def = def; @@ -72,7 +72,7 @@ void Parser::addOption(QString name, QVariant def) throw "Name not unique"; OptionDef *param = new OptionDef; - param->type = OptionType::Option; + param->type = otOption; param->name = name; param->metavar = QString("<%1>").arg(name); param->def = def; @@ -161,7 +161,7 @@ QString Parser::compileHelp(QString progName, int helpIndent, bool useFlags) help << flagPrefix << option->flag << ", "; } help << optPrefix << option->name; - if (option->type == OptionType::Option) + if (option->type == otOption) { QString arg = QString("%1%2").arg(((m_argStyle == ArgumentStyle::Equals) ? "=" : " "), option->metavar); nameLength += arg.length(); @@ -193,7 +193,7 @@ QString Parser::compileUsage(QString progName, bool useFlags) usage << flagPrefix << option->flag; else usage << optPrefix << option->name; - if (option->type == OptionType::Option) + if (option->type == otOption) usage << ((m_argStyle == ArgumentStyle::Equals) ? "=" : " ") << option->metavar; usage << "]"; } @@ -265,9 +265,9 @@ QHash Parser::parse(QStringList argv) throw ParsingError(QString("Option %2%1 was given multiple times").arg(name, optionPrefix)); OptionDef *option = m_options[name]; - if (option->type == OptionType::Switch) + if (option->type == otSwitch) map[name] = true; - else //if (option->type == OptionType::Option) + else //if (option->type == otOption) { if (m_argStyle == ArgumentStyle::Space) expecting.append(name); @@ -312,9 +312,9 @@ QHash Parser::parse(QStringList argv) if (map.contains(option->name)) throw ParsingError(QString("Option %2%1 was given multiple times").arg(option->name, optionPrefix)); - if (option->type == OptionType::Switch) + if (option->type == otSwitch) map[option->name] = true; - else //if (option->type == OptionType::Option) + else //if (option->type == otOption) { if (m_argStyle == ArgumentStyle::Space) expecting.append(option->name); diff --git a/main.cpp b/main.cpp index cf193ff0..31d5277e 100644 --- a/main.cpp +++ b/main.cpp @@ -66,12 +66,9 @@ private: { inst = iter.next(); if (inst->id() == instId) - break; + return inst; } - if (inst->id() != instId) - return InstancePtr(); - else - return iter.peekPrevious(); + return InstancePtr(); } private slots: diff --git a/test.cpp b/test.cpp index 26600220..2b015454 100644 --- a/test.cpp +++ b/test.cpp @@ -10,51 +10,51 @@ using namespace Util::Commandline; int main(int argc, char **argv) { - QCoreApplication app(argc, argv); - app.setApplicationName("MMC Keyring test"); - app.setOrganizationName("Orochimarufan"); - - Parser p; - p.addArgument("user", false); - p.addArgument("password", false); - p.addSwitch("set"); - p.addSwitch("get"); - p.addSwitch("list"); - p.addOption("service", "Test"); - p.addShortOpt("service", 's'); - - QHash args; - try { - args = p.parse(app.arguments()); - } catch (ParsingError) { - std::cout << "Syntax error." << std::endl; - return 1; - } - - if (args["set"].toBool()) { - if (args["user"].isNull() || args["password"].isNull()) { - std::cout << "set operation needs bot user and password set" << std::endl; - return 1; - } - - return Keyring::instance()->storePassword(args["service"].toString(), - args["user"].toString(), args["password"].toString()); - } else if (args["get"].toBool()) { - if (args["user"].isNull()) { - std::cout << "get operation needs user set" << std::endl; - return 1; - } - - std::cout << "Password: " << qPrintable(Keyring::instance()->getPassword(args["service"].toString(), - args["user"].toString())) << std::endl; - return 0; - } else if (args["list"].toBool()) { - QStringList accounts = Keyring::instance()->getStoredAccounts(args["service"].toString()); - std::cout << "stored accounts:" << std::endl << '\t' << qPrintable(accounts.join("\n\t")) << std::endl; - return 0; - } else { - std::cout << "No operation given!" << std::endl; - std::cout << qPrintable(p.compileHelp(argv[0])) << std::endl; - return 1; - } + QCoreApplication app(argc, argv); + app.setApplicationName("MMC Keyring test"); + app.setOrganizationName("Orochimarufan"); + + Parser p; + p.addArgument("user", false); + p.addArgument("password", false); + p.addSwitch("set"); + p.addSwitch("get"); + p.addSwitch("list"); + p.addOption("service", "Test"); + p.addShortOpt("service", 's'); + + QHash args; + try { + args = p.parse(app.arguments()); + } catch (ParsingError) { + std::cout << "Syntax error." << std::endl; + return 1; + } + + if (args["set"].toBool()) { + if (args["user"].isNull() || args["password"].isNull()) { + std::cout << "set operation needs bot user and password set" << std::endl; + return 1; + } + + return Keyring::instance()->storePassword(args["service"].toString(), + args["user"].toString(), args["password"].toString()); + } else if (args["get"].toBool()) { + if (args["user"].isNull()) { + std::cout << "get operation needs user set" << std::endl; + return 1; + } + + std::cout << "Password: " << qPrintable(Keyring::instance()->getPassword(args["service"].toString(), + args["user"].toString())) << std::endl; + return 0; + } else if (args["list"].toBool()) { + QStringList accounts = Keyring::instance()->getStoredAccounts(args["service"].toString()); + std::cout << "stored accounts:" << std::endl << '\t' << qPrintable(accounts.join("\n\t")) << std::endl; + return 0; + } else { + std::cout << "No operation given!" << std::endl; + std::cout << qPrintable(p.compileHelp(argv[0])) << std::endl; + return 1; + } } -- cgit From e4f86893a899ee86cfa6d238f891bec04977c966 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Fri, 22 Mar 2013 14:40:55 +0100 Subject: fix merge issues, make console window work again --- CMakeLists.txt | 2 +- gui/consolewindow.cpp | 77 ++++++++++---------- gui/consolewindow.h | 79 +++++++++----------- libmultimc/include/minecraftprocess.h | 132 ++++++++++++++++++++-------------- libmultimc/src/minecraftprocess.cpp | 22 ++---- libsettings/CMakeLists.txt | 11 ++- libsettings/include/keyring.h | 5 +- libsettings/src/stubkeyring.h | 1 - main.cpp | 1 + 9 files changed, 171 insertions(+), 159 deletions(-) (limited to 'libsettings/include/keyring.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index 742f47f6..97ff786c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,7 +266,7 @@ IF(DEFINED MMC_KEYRING_TEST) # test.cpp ADD_EXECUTABLE(Test test.cpp) QT5_USE_MODULES(Test Core) -TARGET_LINK_LIBRARIES(Test libmmcutil libmmcsettings) +TARGET_LINK_LIBRARIES(Test libUtil libSettings) ENDIF() ################################ INSTALLATION AND PACKAGING ################################ diff --git a/gui/consolewindow.cpp b/gui/consolewindow.cpp index 1d84fe04..811900a2 100644 --- a/gui/consolewindow.cpp +++ b/gui/consolewindow.cpp @@ -4,70 +4,71 @@ #include ConsoleWindow::ConsoleWindow(QWidget *parent) : - QDialog(parent), - ui(new Ui::ConsoleWindow), - m_mayclose(true) + QDialog(parent), + ui(new Ui::ConsoleWindow), + m_mayclose(true) { - ui->setupUi(this); + ui->setupUi(this); } ConsoleWindow::~ConsoleWindow() { - delete ui; + delete ui; } void ConsoleWindow::writeColor(QString text, const char *color) { - // append a paragraph - if (color != nullptr) - ui->text->appendHtml(QString("%2").arg(color).arg(text)); - else - ui->text->appendPlainText(text); - // scroll down - QScrollBar *bar = ui->text->verticalScrollBar(); - bar->setValue(bar->maximum()); + // append a paragraph + if (color != nullptr) + ui->text->appendHtml(QString("%2").arg(color).arg(text)); + else + ui->text->appendPlainText(text); + // scroll down + QScrollBar *bar = ui->text->verticalScrollBar(); + bar->setValue(bar->maximum()); } -void ConsoleWindow::write(QString data, WriteMode mode) +void ConsoleWindow::write(QString data, MessageLevel::Enum mode) { - if (data.endsWith('\n')) - data = data.left(data.length()-1); - QStringList paragraphs = data.split('\n'); - QListIterator iter(paragraphs); - if (mode == MULTIMC) - while(iter.hasNext()) - writeColor(iter.next(), "blue"); - else if (mode == ERROR) - while(iter.hasNext()) - writeColor(iter.next(), "red"); - else - while(iter.hasNext()) - writeColor(iter.next()); + if (data.endsWith('\n')) + data = data.left(data.length()-1); + QStringList paragraphs = data.split('\n'); + QListIterator iter(paragraphs); + if (mode == MessageLevel::MultiMC) + while(iter.hasNext()) + writeColor(iter.next(), "blue"); + else if (mode == MessageLevel::Error) + while(iter.hasNext()) + writeColor(iter.next(), "red"); + // TODO: implement other MessageLevels + else + while(iter.hasNext()) + writeColor(iter.next()); } void ConsoleWindow::clear() { - ui->text->clear(); + ui->text->clear(); } void ConsoleWindow::on_closeButton_clicked() { - close(); + close(); } void ConsoleWindow::setMayClose(bool mayclose) { - m_mayclose = mayclose; - if (mayclose) - ui->closeButton->setEnabled(true); - else - ui->closeButton->setEnabled(false); + m_mayclose = mayclose; + if (mayclose) + ui->closeButton->setEnabled(true); + else + ui->closeButton->setEnabled(false); } void ConsoleWindow::closeEvent(QCloseEvent * event) { - if(!m_mayclose) - event->ignore(); - else - QDialog::closeEvent(event); + if(!m_mayclose) + event->ignore(); + else + QDialog::closeEvent(event); } diff --git a/gui/consolewindow.h b/gui/consolewindow.h index 1d322afb..5490bc92 100644 --- a/gui/consolewindow.h +++ b/gui/consolewindow.h @@ -2,6 +2,7 @@ #define CONSOLEWINDOW_H #include +#include "minecraftprocess.h" namespace Ui { class ConsoleWindow; @@ -9,61 +10,51 @@ class ConsoleWindow; class ConsoleWindow : public QDialog { - Q_OBJECT + Q_OBJECT public: - /** - * @brief The WriteMode enum - * defines how stuff is displayed - */ - enum WriteMode { - DEFAULT, - ERROR, - MULTIMC - }; + explicit ConsoleWindow(QWidget *parent = 0); + ~ConsoleWindow(); - explicit ConsoleWindow(QWidget *parent = 0); - ~ConsoleWindow(); - - /** - * @brief specify if the window is allowed to close - * @param mayclose - * used to keep it alive while MC runs - */ - void setMayClose(bool mayclose); + /** + * @brief specify if the window is allowed to close + * @param mayclose + * used to keep it alive while MC runs + */ + void setMayClose(bool mayclose); public slots: - /** - * @brief write a string - * @param data the string - * @param mode the WriteMode - * lines have to be put through this as a whole! - */ - void write(QString data, WriteMode mode=MULTIMC); - - /** - * @brief write a colored paragraph - * @param data the string - * @param color the css color name - * this will only insert a single paragraph. - * \n are ignored. a real \n is always appended. - */ - void writeColor(QString data, const char *color=nullptr); - - /** - * @brief clear the text widget - */ - void clear(); + /** + * @brief write a string + * @param data the string + * @param mode the WriteMode + * lines have to be put through this as a whole! + */ + void write(QString data, MessageLevel::Enum level=MessageLevel::MultiMC); + + /** + * @brief write a colored paragraph + * @param data the string + * @param color the css color name + * this will only insert a single paragraph. + * \n are ignored. a real \n is always appended. + */ + void writeColor(QString data, const char *color=nullptr); + + /** + * @brief clear the text widget + */ + void clear(); private slots: - void on_closeButton_clicked(); + void on_closeButton_clicked(); protected: - void closeEvent(QCloseEvent *); + void closeEvent(QCloseEvent *); private: - Ui::ConsoleWindow *ui; - bool m_mayclose; + Ui::ConsoleWindow *ui; + bool m_mayclose; }; #endif // CONSOLEWINDOW_H diff --git a/libmultimc/include/minecraftprocess.h b/libmultimc/include/minecraftprocess.h index d6b9f612..f6272183 100644 --- a/libmultimc/include/minecraftprocess.h +++ b/libmultimc/include/minecraftprocess.h @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -23,74 +23,96 @@ #include "libmmc_config.h" +/** + * @brief the MessageLevel Enum + * defines what level a message is + */ +namespace MessageLevel { +enum LIBMULTIMC_EXPORT Enum { + MultiMC, /**< MultiMC Messages */ + Debug, /**< Debug Messages */ + Info, /**< Info Messages */ + Message, /**< Standard Messages */ + Warning, /**< Warnings */ + Error, /**< Errors */ + Fatal /**< Fatal Errors */ +}; +} + /** * @file data/minecraftprocess.h * @brief The MinecraftProcess class */ class LIBMULTIMC_EXPORT MinecraftProcess : public QProcess { - Q_OBJECT + Q_OBJECT public: - /** - * @brief MinecraftProcess constructor - * @param inst the Instance pointer to launch - * @param user the minecraft username - * @param session the minecraft session id - * @param console the instance console window - */ - MinecraftProcess(InstancePtr inst, QString user, QString session); - - /** - * @brief launch minecraft - */ - void launch(); - - /** - * @brief extract the instance icon - * @param inst the instance - * @param destination the destination path - */ - static inline void extractIcon(InstancePtr inst, QString destination); - - /** - * @brief extract the MultiMC launcher.jar - * @param destination the destination path - */ - static inline void extractLauncher(QString destination); - - /** - * @brief prepare the launch by extracting icon and launcher - * @param inst the instance - */ - static void prepare(InstancePtr inst); - - /** - * @brief split a string into argv items like a shell would do - * @param args the argument string - * @return a QStringList containing all arguments - */ - static QStringList splitArgs(QString args); + /** + * @brief MinecraftProcess constructor + * @param inst the Instance pointer to launch + * @param user the minecraft username + * @param session the minecraft session id + * @param console the instance console window + */ + MinecraftProcess(InstancePtr inst, QString user, QString session); + + /** + * @brief launch minecraft + */ + void launch(); + + /** + * @brief extract the instance icon + * @param inst the instance + * @param destination the destination path + */ + static inline void extractIcon(InstancePtr inst, QString destination); + + /** + * @brief extract the MultiMC launcher.jar + * @param destination the destination path + */ + static inline void extractLauncher(QString destination); + + /** + * @brief prepare the launch by extracting icon and launcher + * @param inst the instance + */ + static void prepare(InstancePtr inst); + + /** + * @brief split a string into argv items like a shell would do + * @param args the argument string + * @return a QStringList containing all arguments + */ + static QStringList splitArgs(QString args); signals: - /** - * @brief emitted when mc has finished and the PostLaunchCommand was run - */ - void ended(); + /** + * @brief emitted when mc has finished and the PostLaunchCommand was run + */ + void ended(); + + /** + * @brief emitted when we want to log something + * @param text the text to log + * @param level the level to log at + */ + void log(QString text, MessageLevel::Enum level=MessageLevel::MultiMC); protected: - InstancePtr m_instance; - QString m_user; - QString m_session; - QProcess m_prepostlaunchprocess; - QStringList m_arguments; + InstancePtr m_instance; + QString m_user; + QString m_session; + QProcess m_prepostlaunchprocess; + QStringList m_arguments; - void genArgs(); - void log(QString text, ConsoleWindow::WriteMode mode = ConsoleWindow::MULTIMC); + void genArgs(); protected slots: - void finish(int, QProcess::ExitStatus status); - void on_stdErr(); - void on_stdOut(); + void finish(int, QProcess::ExitStatus status); + void on_stdErr(); + void on_stdOut(); }; diff --git a/libmultimc/src/minecraftprocess.cpp b/libmultimc/src/minecraftprocess.cpp index 3aecb1ee..e22a536c 100644 --- a/libmultimc/src/minecraftprocess.cpp +++ b/libmultimc/src/minecraftprocess.cpp @@ -125,22 +125,12 @@ MinecraftProcess::MinecraftProcess(InstancePtr inst, QString user, QString sessi // console window void MinecraftProcess::on_stdErr() { -// if (m_console != nullptr) -// m_console->write(readAllStandardError(), ConsoleWindow::ERROR); + emit log(readAllStandardError(), MessageLevel::Error); } void MinecraftProcess::on_stdOut() { -// if (m_console != nullptr) -// m_console->write(readAllStandardOutput(), ConsoleWindow::DEFAULT); -} - -void MinecraftProcess::log(QString text) -{ -// if (m_console != nullptr) -// m_console->write(text); -// else - qDebug(qPrintable(text)); + emit log(readAllStandardOutput(), MessageLevel::Message); } // exit handler @@ -151,7 +141,7 @@ void MinecraftProcess::finish(int code, ExitStatus status) //TODO: error handling } - log("Minecraft exited."); + emit log("Minecraft exited."); m_prepostlaunchprocess.processEnvironment().insert("INST_EXITCODE", QString(code)); @@ -191,13 +181,13 @@ void MinecraftProcess::launch() genArgs(); - log(QString("Minecraft folder is: '%1'").arg(workingDirectory())); - log(QString("Instance launched with arguments: '%1'").arg(m_arguments.join("' '"))); + emit log(QString("Minecraft folder is: '%1'").arg(workingDirectory())); + emit log(QString("Instance launched with arguments: '%1'").arg(m_arguments.join("' '"))); start(m_instance->settings().get("JavaPath").toString(), m_arguments); if (!waitForStarted()) { - log("Could not launch minecraft!", ConsoleWindow::ERROR); + emit log("Could not launch minecraft!"); return; //TODO: error handling } diff --git a/libsettings/CMakeLists.txt b/libsettings/CMakeLists.txt index 9ae48354..e5aae0b7 100644 --- a/libsettings/CMakeLists.txt +++ b/libsettings/CMakeLists.txt @@ -18,6 +18,12 @@ include/overridesetting.h include/basicsettingsobject.h include/inisettingsobject.h + +include/keyring.h +) + +SET(LIBSETTINGS_HEADERS_PRIVATE +src/stubkeyring.h ) SET(LIBSETTINGS_SOURCES @@ -29,6 +35,9 @@ src/overridesetting.cpp src/basicsettingsobject.cpp src/inisettingsobject.cpp + +src/keyring.cpp +src/stubkeyring.cpp ) # Set the include dir path. @@ -37,6 +46,6 @@ include_directories(${LIBSETTINGS_INCLUDE_DIR}) add_definitions(-DLIBSETTINGS_LIBRARY) -add_library(libSettings SHARED ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS}) +add_library(libSettings SHARED ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS} ${LIBSETTINGS_HEADERS_PRIVATE}) qt5_use_modules(libSettings Core) target_link_libraries(libSettings) diff --git a/libsettings/include/keyring.h b/libsettings/include/keyring.h index afdc3bba..5774e287 100644 --- a/libsettings/include/keyring.h +++ b/libsettings/include/keyring.h @@ -18,7 +18,7 @@ #ifndef KEYRING_H #define KEYRING_H -#include +#include #include "libsettings_config.h" @@ -31,9 +31,8 @@ * @brief The Keyring class * the System Keyring/Keychain/Wallet/Vault/etc */ -class LIBMMCSETTINGS_EXPORT Keyring : public QObject +class LIBSETTINGS_EXPORT Keyring { - Q_OBJECT public: /** * @brief the System Keyring instance diff --git a/libsettings/src/stubkeyring.h b/libsettings/src/stubkeyring.h index 0566d5ab..b5f04e4c 100644 --- a/libsettings/src/stubkeyring.h +++ b/libsettings/src/stubkeyring.h @@ -24,7 +24,6 @@ class StubKeyring : public Keyring { - Q_OBJECT public: virtual bool storePassword(QString service, QString username, QString password); virtual QString getPassword(QString service, QString username); diff --git a/main.cpp b/main.cpp index 7c82d6d8..4239d70f 100644 --- a/main.cpp +++ b/main.cpp @@ -72,6 +72,7 @@ private slots: //if (instance->getShowConsole()) console->show(); connect(proc, SIGNAL(ended()), SLOT(onTerminated())); + connect(proc, SIGNAL(log(QString,MessageLevel::Enum)), console, SLOT(write(QString,MessageLevel::Enum))); proc->launch(); } -- cgit From 737273348faa598af906bef2d96e6520a85cbc88 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Tue, 26 Mar 2013 17:43:49 +0100 Subject: Use Keyring in the login dialog --- gui/logindialog.cpp | 51 ++++++++++++++++++++++++++++++++++++++++- gui/logindialog.h | 6 ++++- gui/logindialog.ui | 33 ++++++++++++++------------ libsettings/include/keyring.h | 8 +++++++ libsettings/src/stubkeyring.cpp | 6 +++++ libsettings/src/stubkeyring.h | 2 +- 6 files changed, 88 insertions(+), 18 deletions(-) (limited to 'libsettings/include/keyring.h') diff --git a/gui/logindialog.cpp b/gui/logindialog.cpp index 426757a9..842a4541 100644 --- a/gui/logindialog.cpp +++ b/gui/logindialog.cpp @@ -15,12 +15,18 @@ #include "logindialog.h" #include "ui_logindialog.h" +#include "keyring.h" LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) : QDialog(parent), ui(new Ui::LoginDialog) { ui->setupUi(this); + //FIXME: translateable? + ui->usernameTextBox->lineEdit()->setPlaceholderText(QApplication::translate("LoginDialog", "Name", 0)); + + connect(ui->usernameTextBox, SIGNAL(currentTextChanged(QString)), this, SLOT(userTextChanged(QString))); + connect(ui->forgetButton, SIGNAL(clicked(bool)), this, SLOT(forgetCurrentUser())); if (loginErrMsg.isEmpty()) ui->loginErrorLabel->setVisible(false); @@ -33,6 +39,10 @@ LoginDialog::LoginDialog(QWidget *parent, const QString& loginErrMsg) : resize(minimumSizeHint()); layout()->setSizeConstraint(QLayout::SetFixedSize); + Keyring * k = Keyring::instance(); + QStringList accounts = k->getStoredAccounts("minecraft"); + ui->usernameTextBox->addItems(accounts); + } LoginDialog::~LoginDialog() @@ -42,10 +52,49 @@ LoginDialog::~LoginDialog() QString LoginDialog::getUsername() const { - return ui->usernameTextBox->text(); + return ui->usernameTextBox->currentText(); } QString LoginDialog::getPassword() const { return ui->passwordTextBox->text(); } + +void LoginDialog::forgetCurrentUser() +{ + Keyring * k = Keyring::instance(); + QString acct = ui->usernameTextBox->currentText(); + k->removeStoredAccount("minecraft", acct); + ui->passwordTextBox->clear(); + int index = ui->usernameTextBox->findText(acct); + if(index != -1) + ui->usernameTextBox->removeItem(index); +} + +void LoginDialog::userTextChanged ( const QString& user ) +{ + Keyring * k = Keyring::instance(); + QString acct = ui->usernameTextBox->currentText(); + QString passwd = k->getPassword("minecraft",acct); + ui->passwordTextBox->setText(passwd); +} + + +void LoginDialog::accept() +{ + bool saveName = ui->rememberUsernameCheckbox->isChecked(); + bool savePass = ui->rememberPasswordCheckbox->isChecked(); + if(saveName) + { + Keyring * k = Keyring::instance(); + if(savePass) + { + k->storePassword("minecraft",getUsername(),getPassword()); + } + else + { + k->storePassword("minecraft",getUsername(),QString()); + } + } + QDialog::accept(); +} diff --git a/gui/logindialog.h b/gui/logindialog.h index 1b70dcd5..5f4410f5 100644 --- a/gui/logindialog.h +++ b/gui/logindialog.h @@ -32,7 +32,11 @@ public: QString getUsername() const; QString getPassword() const; - + +public slots: + virtual void accept(); + virtual void userTextChanged(const QString& user); + virtual void forgetCurrentUser(); private: Ui::LoginDialog *ui; }; diff --git a/gui/logindialog.ui b/gui/logindialog.ui index ce41d2f5..0aaad52b 100644 --- a/gui/logindialog.ui +++ b/gui/logindialog.ui @@ -6,8 +6,8 @@ 0 0 - 365 - 145 + 476 + 168 @@ -31,9 +31,9 @@ - - - Username + + + true @@ -54,20 +54,23 @@ - - - - - - - - &Force Update + + + + + 0 + 0 + - - true + + Forget + + + + diff --git a/libsettings/include/keyring.h b/libsettings/include/keyring.h index 5774e287..299b14b0 100644 --- a/libsettings/include/keyring.h +++ b/libsettings/include/keyring.h @@ -72,6 +72,14 @@ public: */ virtual QStringList getStoredAccounts(QString service) = 0; + /** + * @brief Remove the specified account from storage + * @param service the service name + * @param username the account name + * @return + */ + virtual void removeStoredAccount(QString service, QString username) = 0; + protected: /// fall back to StubKeyring if false virtual bool isValid() { return false; } diff --git a/libsettings/src/stubkeyring.cpp b/libsettings/src/stubkeyring.cpp index 963f3dd9..cb03bf79 100644 --- a/libsettings/src/stubkeyring.cpp +++ b/libsettings/src/stubkeyring.cpp @@ -90,6 +90,12 @@ QStringList StubKeyring::getStoredAccounts(QString service) return out; } +void StubKeyring::removeStoredAccount ( QString service, QString username ) +{ + QString key = generateKey(service, username); + m_settings.remove(key); +} + StubKeyring::StubKeyring() : m_settings(QSettings::UserScope, "Orochimarufan", "Keyring") { diff --git a/libsettings/src/stubkeyring.h b/libsettings/src/stubkeyring.h index b5f04e4c..45791c85 100644 --- a/libsettings/src/stubkeyring.h +++ b/libsettings/src/stubkeyring.h @@ -29,7 +29,7 @@ public: virtual QString getPassword(QString service, QString username); virtual bool hasPassword(QString service, QString username); virtual QStringList getStoredAccounts(QString service); - + virtual void removeStoredAccount(QString service, QString username); private: friend class Keyring; explicit StubKeyring(); -- cgit