Esempio n. 1
0
void writeOption(QString key, QVariant value)
{
    QVariantMap opt = options();
    if (opt.contains(key))
        opt[key] = value;

    QSqlQuery qry;
    if (qry.exec(QString("update library set options = '%1'").arg(QVariantMapToString(opt))))
        insertLog("library", "update", "1");
}
Esempio n. 2
0
void removeInAppFile(QString filename)
{
    QString id = QFileInfo(filename).baseName();
    QSqlQuery qry;
    if (! qry.exec("delete from files where id = " + id))
        qDebug() << qry.lastError();

    insertLog("files", "delete", id);

    filename = QString("%1/files/%2").arg(dataFolder()).arg(filename);
    if (! QFile::remove(filename))
        qDebug() << "remove file error: " << filename;
}
Esempio n. 3
0
QString MUsers::pay(QString userId, int score)
{
    QSqlQuery qry;

    if ((getScore(userId) - getPayment(userId)) - score < 0)
        return QObject::tr("You have not sufficent score.");

    if (!qry.exec(QString("insert into transactions (user_id, score, description) values (%1, %2, 'pay')").arg(userId).arg(-1 * score)))
        return qry.lastError().text();
    insertLog("transactions", "insert", qry.lastInsertId());

    return "";
}
Esempio n. 4
0
QString MUsers::setPermission(QString userId, QString permission)
{
    QSqlQuery qry;

    // validation
    if (permission != "master")
    {
        qry.exec("select user_id from permissions where permission = 'master' and user_id != "+ userId);
        if (! qry.next())
        {
            qry.exec("select id from permissions");
            if (! qry.next())
                permission = "master";
            else
                return QObject::tr("You must have at least one master user.");
        }
    }

    qry.exec("select id, permission from permissions where user_id = "+ userId);
    if (qry.next())
    {
        QString id = qry.value(0).toString();

        if (! qry.exec(QString("update permissions set permission = '%1' where id = %2").arg(permission).arg(id)))
            return qry.lastError().text();

        insertLog("permissions", "update", id);
    }
    else
    {
        if (! qry.exec(QString("insert into permissions (user_id, permission) values (%1, '%2')").arg(userId).arg(permission)))
            return qry.lastError().text();

        insertLog("permissions", "insert", qry.lastInsertId());
    }

    return "";
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *ui = component.create();
    QObject *button = ui->findChild<QObject*>("saveButton");

    Log log;
    QObject::connect(button, SIGNAL(clicked()),
                         &log, SLOT(insertLog()));

    return app.exec();
}
Esempio n. 6
0
QString MUsers::setPassword(QString userId, QString newPass)
{
    QSqlQuery qry;

    if (newPass.trimmed().isEmpty())
        return QObject::tr("Password is required.");

//        if (! validPassword(newPass))
//            return QObject::tr("Password phrase length must be greater than 6 characters and mustn't be a pure number.");

    QString password = QCryptographicHash::hash(newPass.toUtf8(), QCryptographicHash::Sha1).toHex();
    if (! qry.exec(QString("update users set upassword = '******' where id = %2").arg(password).arg(userId)))
        return qry.lastError().text();

    insertLog("users", "update", userId);

    return "";
}
Esempio n. 7
0
QVariant insertTitleEntry(QString table, QString title)
{
    title = refineText(title);
    QVariant null; null.clear();
    if (title == "") return null;

    QSqlQuery tmp;
    tmp.exec("select id from " + table + " where title = '"+ title +"'");

    if (tmp.next())
        return tmp.value(0);

    QString id;
    StrMap entity;
    entity["title"] = title;
    if (tmp.exec(getReplaceQuery(table, entity, id))) {
        insertLog(table, "insert", id);
        return id;
    }
    return "";
}
Esempio n. 8
0
QString getInAppFilename(QString filename)
{
    if (! filename.startsWith(filesUrl()))
    {
        if (filename.startsWith("file:///"))
            filename = filename.mid(8);

        QString ext = filename.mid(filename.indexOf('.') + 1);

        StrMap file;
        QString fileId;
        file["extension"] = ext;

        QSqlQuery qry;
        if (qry.exec(getReplaceQuery("files", file, fileId)))
        {
            insertLog("files", "insert", fileId);

            QString newfile = QString("%1/files/%2.%3").arg(dataFolder()).arg(fileId).arg(ext);

            if (QFile::exists(newfile))
                QFile::remove(newfile);

            if (QFile::copy(filename, newfile))
                filename = newfile;
            else
            {
                qDebug() << "file copy error: " << filename << newfile;
                return "";
            }
        } else {
            qDebug() << qry.lastError().text();
            return "";
        }
    }

    return QFileInfo(filename).fileName();
}
Esempio n. 9
0
	void onQuit(const UserPtr& user, const std::string& message)
	{
		insertLog(user, "", "QUIT", message);
	}
Esempio n. 10
0
	void onChatAction(const UserPtr& user, const std::string& target, const std::string& message)
	{
		insertLog(user, target, "ACT", message);
	}
Esempio n. 11
0
	void onNick(const UserPtr& user, const std::string& newNick)
	{
		insertLog(user, "", "NICK", newNick);
	}
Esempio n. 12
0
	void onKick(const UserPtr& user, const std::string& channel, const std::string& target, const std::string& message)
	{
		insertLog(user, channel, "KICK", (target + ": " + message));
	}
Esempio n. 13
0
	void onChatText(const UserPtr& user, const std::string& target, const std::string& message)
	{
		insertLog(user, target, "PRIVMSG", message);
	}
Esempio n. 14
0
	void onPart(const UserPtr& user, const std::string& channel, const std::string& message)
	{
		insertLog(user, channel, "PART", message);
	}
Esempio n. 15
0
	void onJoin(const UserPtr& user, const std::string& channel)
	{
		insertLog(user, channel, "JOIN", "");
	}
Esempio n. 16
0
QString MUsers::set(QString userId, StrMap& data)
{
    QSqlQuery qry;

    // validation

    // basic
    if (data["firstname"].toString().isEmpty() || data["lastname"].toString().isEmpty())
        return QObject::tr("User name is required.");
    if (! data["national_id"].toString().isEmpty() && data["national_id"].toInt() == 0)
        return QObject::tr("National id is not valid.");
    if (! (data["birth_date"].toString().isEmpty() || data["birth_date"].toDate().isValid()))
        return QObject::tr("Birth date is not valid.");

    /*
    // used name
    user["firstname"] = refineText(user["firstname"].toString().trimmed());
    user["lastname"] = refineText(user["lastname"].toString().trimmed());
    qry.exec(QString("select id, firstname ||' '|| lastname as name from users where name = '%1'").arg(data["firstname"].toString() +" "+ data["lastname"].toString()));
    if (qry.next())
        if (qry.value(0).toString() != userId)
            return QObject::tr("There is another user with this name.");
    */

    // used national id
    qry.exec("select id, firstname ||' '|| lastname from users where national_id = "+ data["national_id"].toString());
    if (qry.next())
        if (qry.value(0).toString() != userId)
            return QObject::tr("%1 has exact same national id.").arg(qry.value(1).toString());

    // set user label
    if (userId.isEmpty())
        data["label"] = getNewLabel();

    // store
    bool create = userId.isEmpty();
    StrMap permission;
    permission["account_id"] = data.take("account_id");
    permission["label"] = data.take("label");

    if (! qry.exec(getReplaceQuery("users", data, userId)))
        return qry.lastError().text();

    if (create)
        insertLog("users", "insert", userId);
    else
        insertLog("users", "update", userId);

    data["id"] = userId;

    // store permission
    permission["user_id"] = userId;
    if (create)
        permission["permission"] = "user";

    QString permissionId;
    qry.exec("select id from permissions where user_id = "+ userId);
    if (qry.next())
        permissionId = qry.value(0).toString();

    if (! qry.exec(getReplaceQuery("permissions", permission, permissionId)))
        return qry.lastError().text();

    if (permissionId.isEmpty()) {
        permissionId = qry.lastInsertId().toString();
        insertLog("permissions", "insert", permissionId);
    }
    else
        insertLog("permissions", "update", permissionId);

    return "";
}