Start in normal mode if wallet exists. Resolves #9

This commit is contained in:
Ilya Kitaev 2016-06-07 16:26:25 +03:00
parent 493e290956
commit da1b74a707
6 changed files with 120 additions and 245 deletions

View file

@ -44,8 +44,8 @@ int main(int argc, char *argv[])
app.installEventFilter(eventFilter); app.installEventFilter(eventFilter);
qmlRegisterType<clipboardAdapter>("moneroComponents", 1, 0, "Clipboard"); qmlRegisterType<clipboardAdapter>("moneroComponents", 1, 0, "Clipboard");
//qmlRegisterType<Wallet>("moneroWallet", 1, 0, "Wallet"); qmlRegisterInterface<Wallet>("Wallet");
qmlRegisterType<Wallet>();
QQmlApplicationEngine engine; QQmlApplicationEngine engine;

View file

@ -110,6 +110,15 @@ ApplicationWindow {
} }
function walletsFound() {
var wallets = walletManager.findWallets(moneroAccountsDir);
if (wallets.length === 0) {
wallets = walletManager.findWallets(applicationDirectory);
}
print(wallets);
return wallets.length > 0;
}
visible: true visible: true
width: rightPanelExpanded ? 1269 : 1269 - 300 width: rightPanelExpanded ? 1269 : 1269 - 300
height: 800 height: 800
@ -120,6 +129,8 @@ ApplicationWindow {
Component.onCompleted: { Component.onCompleted: {
x = (Screen.width - width) / 2 x = (Screen.width - width) / 2
y = (Screen.height - height) / 2 y = (Screen.height - height) / 2
//
rootItem.state = walletsFound() ? "normal" : "wizard";
} }
Item { Item {

View file

@ -1,194 +1,61 @@
#include "Wallet.h" #include "Wallet.h"
#include "wallet/wallet2_api.h"
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QDebug> #include <QDebug>
#include <QUrl> #include <QUrl>
namespace { namespace {
QString TEST_SEED = "bound class paint gasp task soul forgot past pleasure physical circle "
" appear shore bathroom glove women crap busy beauty bliss idea give needle burden";
namespace {
bool createFileWrapper(const QString &filename)
{
QFile file(filename);
// qDebug("%s: about to create file: %s", __FUNCTION__, qPrintable(filename));
bool result = file.open(QIODevice::WriteOnly);
if (!result ){
qWarning("%s: error creating file '%s' : '%s'",
__FUNCTION__,
qPrintable(filename),
qPrintable(file.errorString()));
}
return result;
}
}
} }
struct WalletImpl
{
QString basename() const;
void setBasename(const QString &name);
QString keysName() const;
QString addressName() const;
// Bitmonero::Wallet * m_walletImpl;
QString m_basename;
QString m_seed;
QString m_password;
QString m_language;
static QString keysName(const QString &basename);
static QString addressName(const QString &basename);
};
QString WalletImpl::basename() const
{
return m_basename;
}
void WalletImpl::setBasename(const QString &name)
{
m_basename = name;
}
QString WalletImpl::keysName() const
{
return keysName(m_basename);
}
QString WalletImpl::addressName() const
{
return addressName(m_basename);
}
QString WalletImpl::keysName(const QString &basename)
{
return basename + ".keys";
}
QString WalletImpl::addressName(const QString &basename)
{
return basename + ".address.txt";
}
Wallet::Wallet(QObject *parent)
: QObject(parent)
{
}
QString Wallet::getSeed() const QString Wallet::getSeed() const
{ {
return m_pimpl->m_seed; return QString::fromStdString(m_walletImpl->seed());
} }
QString Wallet::getSeedLanguage() const QString Wallet::getSeedLanguage() const
{ {
return "English"; return QString::fromStdString(m_walletImpl->getSeedLanguage());
} }
//void Wallet::setSeedLaguage(const QString &lang) int Wallet::status() const
//{
// // TODO: call libwallet's appropriate method
//}
bool Wallet::setPassword(const QString &password)
{ {
// set/change password implies: return m_walletImpl->status();
// recovery wallet with existing path, seed and lang
qDebug("%s: recovering wallet with path=%s, seed=%s, lang=%s and new password=%s",
__FUNCTION__,
qPrintable(this->getBasename()),
qPrintable(this->getSeed()),
qPrintable(this->getSeedLanguage()),
qPrintable(password));
return true;
}
QString Wallet::getPassword() const
{
return m_pimpl->m_password;
}
bool Wallet::rename(const QString &name)
{
QString dst = QUrl(name).toLocalFile();
if (dst.isEmpty())
dst = name;
qDebug("%s: renaming '%s' to '%s'",
__FUNCTION__,
qPrintable(m_pimpl->basename()),
qPrintable(dst));
QString walletKeysFile = m_pimpl->keysName();
QString walletAddressFile = m_pimpl->addressName();
QString dstWalletKeysFile = WalletImpl::keysName(dst);
QString dstWalletAddressFile = WalletImpl::addressName(dst);
QFile walletFile(this->getBasename());
if (!walletFile.rename(dst)) {
qWarning("Error renaming file: '%s' to '%s' : (%s)",
qPrintable(m_pimpl->basename()),
qPrintable(dst),
qPrintable(walletFile.errorString()));
return false;
}
QFile::rename(walletKeysFile, dstWalletKeysFile);
QFile::rename(walletAddressFile, dstWalletAddressFile);
bool result = QFile::exists(dst) && QFile::exists(dstWalletKeysFile)
&& QFile::exists(dstWalletAddressFile);
if (result) {
m_pimpl->m_basename = dst;
}
return result;
}
QString Wallet::getBasename() const
{
return m_pimpl->basename();
}
int Wallet::error() const
{
return 0;
} }
QString Wallet::errorString() const QString Wallet::errorString() const
{ {
return m_pimpl->m_seed; return QString::fromStdString(m_walletImpl->errorString());
} }
Wallet::Wallet(const QString &path, const QString &password, const QString &language) bool Wallet::setPassword(const QString &password)
{ {
m_pimpl = new WalletImpl; return m_walletImpl->setPassword(password.toStdString());
m_pimpl->m_basename = path; }
m_pimpl->m_password = password;
m_pimpl->m_language = language;
m_pimpl->m_seed = TEST_SEED;
// Create dummy files for testing QString Wallet::address() const
QFileInfo fi(path); {
QDir tempDir; return QString::fromStdString(m_walletImpl->address());
tempDir.mkpath(fi.absolutePath()); }
createFileWrapper(m_pimpl->basename());
createFileWrapper(m_pimpl->keysName()); bool Wallet::store(const QString &path)
createFileWrapper(m_pimpl->addressName()); {
return m_walletImpl->store(path.toStdString());
} }
Wallet::Wallet(Bitmonero::Wallet *w, QObject *parent)
: QObject(parent), m_walletImpl(w)
{
}
Wallet::~Wallet()
{
Bitmonero::WalletManagerFactory::getWalletManager()->closeWallet(m_walletImpl);
}

View file

@ -3,13 +3,18 @@
#include <QObject> #include <QObject>
struct WalletImpl; namespace Bitmonero {
class Wallet; // forward declaration
}
class Wallet : public QObject class Wallet : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString seed READ getSeed) Q_PROPERTY(QString seed READ getSeed)
public: public:
explicit Wallet(QObject *parent = 0); enum Status {
Status_Ok = 0,
Status_Error = 1
};
//! returns mnemonic seed //! returns mnemonic seed
Q_INVOKABLE QString getSeed() const; Q_INVOKABLE QString getSeed() const;
@ -17,28 +22,31 @@ public:
//! returns seed language //! returns seed language
Q_INVOKABLE QString getSeedLanguage() const; Q_INVOKABLE QString getSeedLanguage() const;
//! returns last operation's status
Q_INVOKABLE int status() const;
//! returns last operation's error message
Q_INVOKABLE QString errorString() const;
//! changes the password using existing parameters (path, seed, seed lang) //! changes the password using existing parameters (path, seed, seed lang)
Q_INVOKABLE bool setPassword(const QString &password); Q_INVOKABLE bool setPassword(const QString &password);
//! returns curret wallet password
Q_INVOKABLE QString getPassword() const;
//! renames/moves wallet files //! returns wallet's public address
Q_INVOKABLE bool rename(const QString &name); Q_INVOKABLE QString address() const;
//! saves wallet to the file by given path
Q_INVOKABLE bool store(const QString &path);
//! returns current wallet name (basename, as wallet consists of several files)
Q_INVOKABLE QString getBasename() const;
Q_INVOKABLE int error() const;
Q_INVOKABLE QString errorString() const;
private: private:
Wallet(const QString &path, const QString &password, const QString &language); Wallet(Bitmonero::Wallet *w, QObject * parent = 0);
~Wallet();
private: private:
friend class WalletManager; friend class WalletManager;
//! pimpl wrapper for libwallet; //! libwallet's
WalletImpl * m_pimpl; Bitmonero::Wallet * m_walletImpl;
}; };
#endif // WALLET_H #endif // WALLET_H

View file

@ -7,6 +7,8 @@
#include <QDebug> #include <QDebug>
#include <QUrl> #include <QUrl>
WalletManager * WalletManager::m_instance = nullptr; WalletManager * WalletManager::m_instance = nullptr;
@ -18,89 +20,55 @@ WalletManager *WalletManager::instance()
if (!m_instance) { if (!m_instance) {
m_instance = new WalletManager; m_instance = new WalletManager;
} }
// Checking linkage (doesn't work, TODO: have every dependencies linked statically into libwallet)
Bitmonero::WalletManager * wallet_manager_impl = Bitmonero::WalletManagerFactory::getWalletManager();
return m_instance; return m_instance;
} }
Wallet *WalletManager::createWallet(const QString &path, const QString &password, Wallet *WalletManager::createWallet(const QString &path, const QString &password,
const QString &language) const QString &language, bool testnet)
{ {
QFileInfo fi(path); Bitmonero::Wallet * w = m_pimpl->createWallet(path.toStdString(), password.toStdString(),
if (fi.exists()) { language.toStdString(), testnet);
qCritical("%s: already exists", __FUNCTION__); Wallet * wallet = new Wallet(w);
// TODO: set error and error string
// return nullptr;
}
Wallet * wallet = new Wallet(path, password, language);
return wallet; return wallet;
} }
Wallet *WalletManager::openWallet(const QString &path, const QString &language, const QString &password) Wallet *WalletManager::openWallet(const QString &path, const QString &password, bool testnet)
{ {
QFileInfo fi(path);
if (fi.exists()) {
qCritical("%s: not exists", __FUNCTION__);
// TODO: set error and error string
// return nullptr;
}
// TODO: call the libwallet api here; // TODO: call the libwallet api here;
Wallet * wallet = new Wallet(path, password, language);
Bitmonero::Wallet * w = m_pimpl->openWallet(path.toStdString(), password.toStdString(), testnet);
Wallet * wallet = new Wallet(w);
return wallet; return wallet;
} }
Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, const QString &language)
Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, bool testnet)
{ {
// TODO: call the libwallet api here; Bitmonero::Wallet * w = m_pimpl->recoveryWallet(path.toStdString(), memo.toStdString(), testnet);
Wallet * wallet = new Wallet(w);
return nullptr; return wallet;
} }
bool WalletManager::moveWallet(const QString &src, const QString &dst_)
{
// TODO: move this to libwallet;
QFile walletFile(src);
if (!walletFile.exists()) {
qWarning("%s: source file [%s] doesn't exits", __FUNCTION__,
qPrintable(src));
return false;
}
QString dst = QUrl(dst_).toLocalFile();
QString walletKeysFile = src + ".keys";
QString walletAddressFile = src + ".address.txt";
QString dstWalletKeysFile = dst + ".keys";
QString dstWalletAddressFile = dst + ".address.txt";
if (!walletFile.rename(dst)) {
qWarning("Error renaming file: '%s' to '%s' : (%s)",
qPrintable(src),
qPrintable(dst),
qPrintable(walletFile.errorString()));
return false;
}
QFile::rename(walletKeysFile, dstWalletKeysFile);
QFile::rename(walletAddressFile, dstWalletAddressFile);
return QFile::exists(dst) && QFile::exists(dstWalletKeysFile)
&& QFile::exists(dstWalletAddressFile);
}
void WalletManager::closeWallet(Wallet *wallet) void WalletManager::closeWallet(Wallet *wallet)
{ {
delete wallet; delete wallet;
} }
QString WalletManager::walletLanguage(const QString &locale) bool WalletManager::walletExists(const QString &path) const
{ {
return "English"; return m_pimpl->walletExists(path.toStdString());
} }
int WalletManager::error() const QStringList WalletManager::findWallets(const QString &path)
{ {
return 0; std::vector<std::string> found_wallets = m_pimpl->findWallets(path.toStdString());
QStringList result;
for (const auto &w : found_wallets) {
result.append(QString::fromStdString(w));
}
return result;
} }
QString WalletManager::errorString() const QString WalletManager::errorString() const
@ -108,9 +76,21 @@ QString WalletManager::errorString() const
return tr("Unknown error"); return tr("Unknown error");
} }
WalletManager::WalletManager(QObject *parent) : QObject(parent) bool WalletManager::moveWallet(const QString &src, const QString &dst)
{ {
return true;
}
QString WalletManager::walletLanguage(const QString &locale)
{
return "English";
}
WalletManager::WalletManager(QObject *parent) : QObject(parent)
{
m_pimpl = Bitmonero::WalletManagerFactory::getWalletManager();
} }

View file

@ -4,6 +4,9 @@
#include <QObject> #include <QObject>
class Wallet; class Wallet;
namespace Bitmonero {
class WalletManager;
}
class WalletManager : public QObject class WalletManager : public QObject
{ {
@ -12,37 +15,43 @@ public:
static WalletManager * instance(); static WalletManager * instance();
// wizard: createWallet path; // wizard: createWallet path;
Q_INVOKABLE Wallet * createWallet(const QString &path, const QString &password, Q_INVOKABLE Wallet * createWallet(const QString &path, const QString &password,
const QString &language); const QString &language, bool testnet = false);
// just for future use // just for future use
Q_INVOKABLE Wallet * openWallet(const QString &path, const QString &language, Q_INVOKABLE Wallet * openWallet(const QString &path, const QString &password, bool testnet = false);
const QString &password);
// wizard: recoveryWallet path; hint: internally it recorvers wallet and set password = "" // wizard: recoveryWallet path; hint: internally it recorvers wallet and set password = ""
Q_INVOKABLE Wallet * recoveryWallet(const QString &path, const QString &memo, Q_INVOKABLE Wallet * recoveryWallet(const QString &path, const QString &memo,
const QString &language); bool testnet = false);
// wizard: both "create" and "recovery" paths.
// TODO: probably move it to "Wallet" interface
Q_INVOKABLE bool moveWallet(const QString &src, const QString &dst);
//! utils: close wallet to free memory //! utils: close wallet to free memory
Q_INVOKABLE void closeWallet(Wallet * wallet); Q_INVOKABLE void closeWallet(Wallet * wallet);
//! returns libwallet language name for given locale //! checks is given filename is a wallet;
Q_INVOKABLE QString walletLanguage(const QString &locale); Q_INVOKABLE bool walletExists(const QString &path) const;
//! returns last error happened in WalletManager //! returns list with wallet's filenames, if found by given path
Q_INVOKABLE int error() const; Q_INVOKABLE QStringList findWallets(const QString &path);
//! returns error description in human language //! returns error description in human language
Q_INVOKABLE QString errorString() const; Q_INVOKABLE QString errorString() const;
// wizard: both "create" and "recovery" paths.
// TODO: probably move it to "Wallet" interface
Q_INVOKABLE bool moveWallet(const QString &src, const QString &dst);
//! returns libwallet language name for given locale
Q_INVOKABLE QString walletLanguage(const QString &locale);
signals: signals:
public slots: public slots:
private: private:
explicit WalletManager(QObject *parent = 0); explicit WalletManager(QObject *parent = 0);
static WalletManager * m_instance; static WalletManager * m_instance;
Bitmonero::WalletManager * m_pimpl;
}; };
#endif // WALLETMANAGER_H #endif // WALLETMANAGER_H