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);
qmlRegisterType<clipboardAdapter>("moneroComponents", 1, 0, "Clipboard");
//qmlRegisterType<Wallet>("moneroWallet", 1, 0, "Wallet");
qmlRegisterType<Wallet>();
qmlRegisterInterface<Wallet>("Wallet");
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
width: rightPanelExpanded ? 1269 : 1269 - 300
height: 800
@ -120,6 +129,8 @@ ApplicationWindow {
Component.onCompleted: {
x = (Screen.width - width) / 2
y = (Screen.height - height) / 2
//
rootItem.state = walletsFound() ? "normal" : "wizard";
}
Item {

View File

@ -1,194 +1,61 @@
#include "Wallet.h"
#include "wallet/wallet2_api.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QUrl>
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
{
return m_pimpl->m_seed;
return QString::fromStdString(m_walletImpl->seed());
}
QString Wallet::getSeedLanguage() const
{
return "English";
return QString::fromStdString(m_walletImpl->getSeedLanguage());
}
//void Wallet::setSeedLaguage(const QString &lang)
//{
// // TODO: call libwallet's appropriate method
//}
bool Wallet::setPassword(const QString &password)
int Wallet::status() const
{
// set/change password implies:
// 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;
return m_walletImpl->status();
}
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;
m_pimpl->m_basename = path;
m_pimpl->m_password = password;
m_pimpl->m_language = language;
m_pimpl->m_seed = TEST_SEED;
return m_walletImpl->setPassword(password.toStdString());
}
// Create dummy files for testing
QFileInfo fi(path);
QDir tempDir;
tempDir.mkpath(fi.absolutePath());
createFileWrapper(m_pimpl->basename());
createFileWrapper(m_pimpl->keysName());
createFileWrapper(m_pimpl->addressName());
QString Wallet::address() const
{
return QString::fromStdString(m_walletImpl->address());
}
bool Wallet::store(const QString &path)
{
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>
struct WalletImpl;
namespace Bitmonero {
class Wallet; // forward declaration
}
class Wallet : public QObject
{
Q_OBJECT
Q_PROPERTY(QString seed READ getSeed)
public:
explicit Wallet(QObject *parent = 0);
enum Status {
Status_Ok = 0,
Status_Error = 1
};
//! returns mnemonic seed
Q_INVOKABLE QString getSeed() const;
@ -17,28 +22,31 @@ public:
//! returns seed language
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)
Q_INVOKABLE bool setPassword(const QString &password);
//! returns curret wallet password
Q_INVOKABLE QString getPassword() const;
//! renames/moves wallet files
Q_INVOKABLE bool rename(const QString &name);
//! returns wallet's public address
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:
Wallet(const QString &path, const QString &password, const QString &language);
Wallet(Bitmonero::Wallet *w, QObject * parent = 0);
~Wallet();
private:
friend class WalletManager;
//! pimpl wrapper for libwallet;
WalletImpl * m_pimpl;
//! libwallet's
Bitmonero::Wallet * m_walletImpl;
};
#endif // WALLET_H

View File

@ -7,6 +7,8 @@
#include <QDebug>
#include <QUrl>
WalletManager * WalletManager::m_instance = nullptr;
@ -18,89 +20,55 @@ WalletManager *WalletManager::instance()
if (!m_instance) {
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;
}
Wallet *WalletManager::createWallet(const QString &path, const QString &password,
const QString &language)
const QString &language, bool testnet)
{
QFileInfo fi(path);
if (fi.exists()) {
qCritical("%s: already exists", __FUNCTION__);
// TODO: set error and error string
// return nullptr;
}
Wallet * wallet = new Wallet(path, password, language);
Bitmonero::Wallet * w = m_pimpl->createWallet(path.toStdString(), password.toStdString(),
language.toStdString(), testnet);
Wallet * wallet = new Wallet(w);
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;
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;
}
Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, const QString &language)
{
// TODO: call the libwallet api here;
return nullptr;
Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, bool testnet)
{
Bitmonero::Wallet * w = m_pimpl->recoveryWallet(path.toStdString(), memo.toStdString(), testnet);
Wallet * wallet = new Wallet(w);
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)
{
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
@ -108,9 +76,21 @@ QString WalletManager::errorString() const
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>
class Wallet;
namespace Bitmonero {
class WalletManager;
}
class WalletManager : public QObject
{
@ -12,37 +15,43 @@ public:
static WalletManager * instance();
// wizard: createWallet path;
Q_INVOKABLE Wallet * createWallet(const QString &path, const QString &password,
const QString &language);
const QString &language, bool testnet = false);
// just for future use
Q_INVOKABLE Wallet * openWallet(const QString &path, const QString &language,
const QString &password);
Q_INVOKABLE Wallet * openWallet(const QString &path, const QString &password, bool testnet = false);
// wizard: recoveryWallet path; hint: internally it recorvers wallet and set password = ""
Q_INVOKABLE Wallet * recoveryWallet(const QString &path, const QString &memo,
const QString &language);
// wizard: both "create" and "recovery" paths.
// TODO: probably move it to "Wallet" interface
Q_INVOKABLE bool moveWallet(const QString &src, const QString &dst);
bool testnet = false);
//! utils: close wallet to free memory
Q_INVOKABLE void closeWallet(Wallet * wallet);
//! returns libwallet language name for given locale
Q_INVOKABLE QString walletLanguage(const QString &locale);
//! checks is given filename is a wallet;
Q_INVOKABLE bool walletExists(const QString &path) const;
//! returns last error happened in WalletManager
Q_INVOKABLE int error() const;
//! returns list with wallet's filenames, if found by given path
Q_INVOKABLE QStringList findWallets(const QString &path);
//! returns error description in human language
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:
public slots:
private:
explicit WalletManager(QObject *parent = 0);
static WalletManager * m_instance;
Bitmonero::WalletManager * m_pimpl;
};
#endif // WALLETMANAGER_H