simple login handling

This commit is contained in:
Thomas Lindner 2022-11-16 00:29:32 +01:00
parent eef181434c
commit c4a75d35df
10 changed files with 241 additions and 8 deletions

View file

@ -70,7 +70,7 @@ EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false ExperimentalAutoDetectBinPacking: false
BasedOnStyle: '' BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerAllOnOneLineOrOnePerLine: true
AllowAllConstructorInitializersOnNextLine: true AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true FixNamespaceComments: true
ForEachMacros: ForEachMacros:

View file

@ -1,5 +1,12 @@
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
deltachat_context.cc
deltachat_context.hh
login_dialog.cc
login_dialog.hh
main.cc main.cc
main.hh
main_window.cc
main_window.hh
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE PRIVATE
@ -12,7 +19,7 @@ target_compile_features(${PROJECT_NAME}
) )
target_compile_options(${PROJECT_NAME} target_compile_options(${PROJECT_NAME}
PRIVATE PRIVATE
$<$<OR:$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:AppleClang>,$<C_COMPILER_ID:GNU>>:-Wall -Wextra> $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall -Wextra>
) )
set_target_properties(${PROJECT_NAME} PROPERTIES set_target_properties(${PROJECT_NAME} PROPERTIES
WIN32_EXECUTABLE ON WIN32_EXECUTABLE ON

51
src/deltachat_context.cc Normal file
View file

@ -0,0 +1,51 @@
#include "deltachat_context.hh"
#include <QApplication>
#include <iostream>
#include <utility>
namespace kappachat {
DeltachatContext::DeltachatContext()
: m_context{dc_context_new(NULL, "kappachat.sqlite", NULL)},
m_event_thread{std::bind(&DeltachatContext::eventThread, this)} {}
DeltachatContext::~DeltachatContext() {
dc_stop_io(m_context);
dc_context_unref(m_context);
m_event_thread.join();
}
void DeltachatContext::setConfig(QString key, QString value) {
dc_set_config(m_context, key.toStdString().c_str(),
value.toStdString().c_str());
}
bool DeltachatContext::isConfigured() const {
return dc_is_configured(m_context);
}
void DeltachatContext::configure() {
dc_configure(m_context);
}
void DeltachatContext::eventThread() {
dc_event_emitter_t *emitter = dc_get_event_emitter(m_context);
while (dc_event_t *event = dc_get_next_event(emitter)) {
switch (dc_event_get_id(event)) {
case DC_EVENT_CONFIGURE_PROGRESS: {
int permille = dc_event_get_data1_int(event);
char *message = dc_event_get_data2_str(event);
emit configureProgress(permille, message ?: "");
dc_str_unref(message);
dc_event_unref(event);
break;
}
default:
dc_event_unref(event);
}
}
dc_event_emitter_unref(emitter);
}
} // namespace kappachat

34
src/deltachat_context.hh Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include <deltachat.h>
#include <QObject>
#include <QString>
#include <thread>
namespace kappachat {
class DeltachatContext : public QObject {
Q_OBJECT
public:
DeltachatContext();
~DeltachatContext();
bool isConfigured() const;
public slots:
void setConfig(QString key, QString value);
void configure();
signals:
void configureProgress(int permille, QString message);
private:
void eventThread();
dc_context_t *m_context;
std::thread m_event_thread;
};
} // namespace kappachat

63
src/login_dialog.cc Normal file
View file

@ -0,0 +1,63 @@
#include "login_dialog.hh"
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QLabel>
#include <QMessageBox>
#include <QVBoxLayout>
namespace kappachat {
LoginDialog::LoginDialog(QWidget *parent, DeltachatContext *context)
: QDialog{parent},
m_context{context},
m_email{new QLineEdit},
m_password{new QLineEdit} {
connect(m_context, &DeltachatContext::configureProgress, this,
&LoginDialog::configureProgress);
auto form = new QFormLayout;
form->addRow(new QLabel{"Email:"}, m_email);
form->addRow(new QLabel{"Password:"}, m_password);
m_password->setEchoMode(QLineEdit::Password);
auto buttons =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
auto vbox = new QVBoxLayout{this};
vbox->addLayout(form);
vbox->addWidget(buttons);
setWindowTitle("Login");
setModal(true);
if (!m_context->isConfigured()) {
show();
}
}
void LoginDialog::accept() {
m_context->setConfig("addr", m_email->text());
m_context->setConfig("mail_pw", m_password->text());
m_context->configure();
QDialog::accept();
}
void LoginDialog::configureProgress(int permille, QString message) {
if (!permille) {
QMessageBox messagebox;
messagebox.setText("Login failed.");
if (message.length() > 500) {
messagebox.setDetailedText(message);
} else {
messagebox.setInformativeText(message);
}
messagebox.setIcon(QMessageBox::Critical);
messagebox.exec();
show();
}
}
} // namespace kappachat

25
src/login_dialog.hh Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include "deltachat_context.hh"
#include <QDialog>
#include <QLineEdit>
namespace kappachat {
class LoginDialog : public QDialog {
Q_OBJECT
public:
LoginDialog(QWidget *parent, DeltachatContext *context);
public slots:
void accept() override;
void configureProgress(int permille, QString message);
private:
DeltachatContext *m_context;
QLineEdit *m_email;
QLineEdit *m_password;
};
} // namespace kappachat

View file

@ -1,10 +1,16 @@
#include <QApplication> #include "main.hh"
#include <QMainWindow>
int main(int argc, char **argv) { int main(int argc, char **argv) {
QApplication app{argc, argv}; kappachat::Kappachat app{argc, argv};
QMainWindow mainwindow;
mainwindow.show();
return app.exec(); return app.exec();
} }
namespace kappachat {
Kappachat::Kappachat(int argc, char **argv)
: QApplication{argc, argv}, login_dialog{&main_window, &context} {
connect(&login_dialog, &LoginDialog::rejected, &main_window,
&MainWindow::close);
}
} // namespace kappachat

22
src/main.hh Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include "deltachat_context.hh"
#include "login_dialog.hh"
#include "main_window.hh"
#include <QApplication>
namespace kappachat {
class Kappachat : public QApplication {
Q_OBJECT
public:
Kappachat(int argc, char **argv);
private:
DeltachatContext context;
MainWindow main_window;
LoginDialog login_dialog;
};
} // namespace kappachat

9
src/main_window.cc Normal file
View file

@ -0,0 +1,9 @@
#include "main_window.hh"
namespace kappachat {
MainWindow::MainWindow() {
showMaximized();
}
} // namespace kappachat

16
src/main_window.hh Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include <QMainWindow>
namespace kappachat {
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
private:
};
} // namespace kappachat