Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: use search settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TriKriSta committed Jun 24, 2018
1 parent 87b340f commit 610e04a
Show file tree
Hide file tree
Showing 24 changed files with 625 additions and 103 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ set(${PROJECT_NAME}_SOURCES
src/widget/flowlayout.h
src/widget/searchform.cpp
src/widget/searchform.h
src/widget/searchtypes.h
src/widget/form/addfriendform.cpp
src/widget/form/addfriendform.h
src/widget/form/chatform.cpp
Expand Down
8 changes: 8 additions & 0 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ ChatLine::Ptr ChatLog::getLatestLine() const
return nullptr;
}

ChatLine::Ptr ChatLog::getFirstLine() const
{
if (!lines.empty()) {
return lines.first();
}
return nullptr;
}

/**
* @brief Finds the chat line object at a position on screen
* @param pos Position on screen in global coordinates
Expand Down
1 change: 1 addition & 0 deletions src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ChatLog : public QGraphicsView
ChatLine::Ptr getTypingNotification() const;
QVector<ChatLine::Ptr> getLines();
ChatLine::Ptr getLatestLine() const;
ChatLine::Ptr getFirstLine() const;
ChatLineContent* getContentFromGlobalPos(QPoint pos) const;
const uint repNameAfter = 5 * 60;

Expand Down
42 changes: 30 additions & 12 deletions src/chatlog/content/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,30 @@ void Text::setText(const QString& txt)
dirty = true;
}

void Text::selectText(const QString &txt, const int index)
void Text::selectText(const QString& txt, const std::pair<int, int>& point)
{
regenerate();

if (!doc) {
return;
}

auto cursor = doc->find(txt, index);
auto cursor = doc->find(txt, point.first);

if (!cursor.isNull()) {
cursor.beginEditBlock();
cursor.setPosition(index);
cursor.setPosition(index + txt.size(), QTextCursor::KeepAnchor);
cursor.endEditBlock();
selectText(cursor, point);
}

QTextCharFormat format;
format.setBackground(QBrush(QColor("#ff7626")));
cursor.mergeCharFormat(format);
void Text::selectText(const QRegExp &exp, const std::pair<int, int>& point)
{
regenerate();

regenerate();
update();
if (!doc) {
return;
}

auto cursor = doc->find(exp, point.first);

selectText(cursor, point);
}

void Text::deselectText()
Expand Down Expand Up @@ -439,3 +440,20 @@ QString Text::extractImgTooltip(int pos) const

return QString();
}

void Text::selectText(QTextCursor& cursor, const std::pair<int, int>& point)
{
if (!cursor.isNull()) {
cursor.beginEditBlock();
cursor.setPosition(point.first);
cursor.setPosition(point.first + point.second, QTextCursor::KeepAnchor);
cursor.endEditBlock();

QTextCharFormat format;
format.setBackground(QBrush(QColor("#ff7626")));
cursor.mergeCharFormat(format);

regenerate();
update();
}
}
5 changes: 4 additions & 1 deletion src/chatlog/content/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Text : public ChatLineContent
virtual ~Text();

void setText(const QString& txt);
void selectText(const QString& txt, const int index);
void selectText(const QString& txt, const std::pair<int, int>& point);
void selectText(const QRegExp& exp, const std::pair<int, int>& point);
void deselectText();

virtual void setWidth(qreal width) final;
Expand Down Expand Up @@ -78,6 +79,8 @@ class Text : public ChatLineContent
QString extractImgTooltip(int pos) const;

private:
void selectText(QTextCursor& cursor, const std::pair<int, int>& point);

QTextDocument* doc = nullptr;
QString text;
QString rawText;
Expand Down
40 changes: 40 additions & 0 deletions src/persistence/db/rawdatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ bool RawDatabase::open(const QString& path, const QString& hexKey)
return false;
}

if (sqlite3_create_function(sqlite, "regexp", 2, SQLITE_UTF8, NULL, &RawDatabase::regexpInsensitive, NULL, NULL)) {
qWarning() << "Failed to create function regexp";
close();
return false;
}

if (sqlite3_create_function(sqlite, "regexpsensitive", 2, SQLITE_UTF8, NULL, &RawDatabase::regexpSensitive, NULL, NULL)) {
qWarning() << "Failed to create function regexpsensitive";
close();
return false;
}

if (!hexKey.isEmpty()) {
if (!execNow("PRAGMA key = \"x'" + hexKey + "'\"")) {
qWarning() << "Failed to set encryption key";
Expand Down Expand Up @@ -705,3 +717,31 @@ QVariant RawDatabase::extractData(sqlite3_stmt* stmt, int col)
return QByteArray::fromRawData(data, len);
}
}

void RawDatabase::regexpInsensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
regexp(ctx, argc, argv, Qt::CaseInsensitive);
}

void RawDatabase::regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
regexp(ctx, argc, argv, Qt::CaseSensitive);
}

void RawDatabase::regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const Qt::CaseSensitivity cs)
{
QRegExp regex;
QString str1((const char*)sqlite3_value_text(argv[0]));
QString str2((const char*)sqlite3_value_text(argv[1]));

regex.setPattern(str1);
regex.setCaseSensitivity(cs);

bool b = str2.contains(regex);

if (b) {
sqlite3_result_int(ctx, 1);
} else {
sqlite3_result_int(ctx, 0);
}
}
5 changes: 5 additions & 0 deletions src/persistence/db/rawdatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

struct sqlite3;
struct sqlite3_stmt;
struct sqlite3_context;
struct sqlite3_value;

class RawDatabase : QObject
{
Expand Down Expand Up @@ -85,6 +87,9 @@ protected slots:
static QString deriveKey(const QString& password, const QByteArray& salt);
static QString deriveKey(const QString& password);
static QVariant extractData(sqlite3_stmt* stmt, int col);
static void regexpInsensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv);
static void regexpSensitive(sqlite3_context* ctx, int argc, sqlite3_value** argv);
static void regexp(sqlite3_context* ctx, int argc, sqlite3_value** argv, const Qt::CaseSensitivity cs);

private:
struct Transaction
Expand Down
77 changes: 72 additions & 5 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ QList<History::DateMessages> History::getChatHistoryCounts(const ToxPk& friendPk
return counts;
}

QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTime& from, QString phrase)
QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTime& from, QString phrase, const ParameterSearch& parameter)
{
QList<QDateTime> counts;
auto rowCallback = [&counts](const QVector<QVariant>& row) {
Expand All @@ -324,18 +324,85 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi

phrase.replace("'", "''");

QString message;

switch (parameter.filter) {
case FilterSearch::Register:
message = QString("message LIKE '%%1%'").arg(phrase);
break;
case FilterSearch::WordsOnly:
message = QString("message REGEXP '\\b%1\\b'").arg(phrase.toLower());
break;
case FilterSearch::RegisterAndWordsOnly:
message = QString("REGEXPSENSITIVE(message, '\\b%1\\b')").arg(phrase);
break;
case FilterSearch::Regular:
message = QString("message REGEXP '%1'").arg(phrase);
break;
case FilterSearch::RegisterAndRegular:
message = QString("REGEXPSENSITIVE(message '%1')").arg(phrase);
break;
default:
message = QString("LOWER(message) LIKE '%%1%'").arg(phrase.toLower());
break;
}

QDateTime date = from;
if (parameter.period != PeriodSearch::None) {
date = QDateTime(parameter.date);
}

QString period;
switch (parameter.period) {
case PeriodSearch::WithTheFirst:
period = QString("ORDER BY timestamp ASC LIMIT 1;");
break;
case PeriodSearch::AfterDate:
period = QString("AND timestamp > '%1' ORDER BY timestamp ASC LIMIT 1;").arg(date.toMSecsSinceEpoch());
break;
case PeriodSearch::BeforeDate:
period = QString("AND timestamp < '%1' ORDER BY timestamp DESC LIMIT 1;").arg(date.toMSecsSinceEpoch());
break;
default:
period = QString("AND timestamp < '%1' ORDER BY timestamp DESC LIMIT 1;").arg(date.toMSecsSinceEpoch());
break;
}

QString queryText =
QString("SELECT timestamp "
"FROM history "
"LEFT JOIN faux_offline_pending ON history.id = faux_offline_pending.id "
"JOIN peers chat ON chat_id = chat.id "
"WHERE chat.public_key='%1' "
"AND message LIKE '%%2%' "
"AND timestamp < '%3' ORDER BY timestamp DESC LIMIT 1;")
"AND %2 "
"%3")
.arg(friendPk)
.arg(phrase)
.arg(from.toMSecsSinceEpoch());
.arg(message)
.arg(period);


db->execNow({queryText, rowCallback});
if (!counts.isEmpty()) {
return counts[0];
}

return QDateTime();
}

QDateTime History::getStartDateChatHistory(const QString &friendPk)
{
QList<QDateTime> counts;
auto rowCallback = [&counts](const QVector<QVariant>& row) {
counts.append(QDateTime::fromMSecsSinceEpoch(row[0].toLongLong()));
};

QString queryText =
QString("SELECT timestamp "
"FROM history "
"LEFT JOIN faux_offline_pending ON history.id = faux_offline_pending.id "
"JOIN peers chat ON chat_id = chat.id "
"WHERE chat.public_key='%1' ORDER BY timestamp ASC LIMIT 1;")
.arg(friendPk);

db->execNow({queryText, rowCallback});

Expand Down
4 changes: 3 additions & 1 deletion src/persistence/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "src/core/toxpk.h"
#include "src/persistence/db/rawdatabase.h"
#include "src/widget/searchtypes.h"

class Profile;
class HistoryKeeper;
Expand Down Expand Up @@ -82,7 +83,8 @@ class History
const QDateTime& to);
QList<HistMessage> getChatHistoryDefaultNum(const QString& friendPk);
QList<DateMessages> getChatHistoryCounts(const ToxPk& friendPk, const QDate& from, const QDate& to);
QDateTime getDateWhereFindPhrase(const QString& friendPk, const QDateTime& from, QString phrase);
QDateTime getDateWhereFindPhrase(const QString& friendPk, const QDateTime& from, QString phrase, const ParameterSearch &parameter);
QDateTime getStartDateChatHistory(const QString& friendPk);

void markAsSent(qint64 messageId);

Expand Down
Loading

0 comments on commit 610e04a

Please sign in to comment.