Skip to content

Commit

Permalink
Features a font download from update server
Browse files Browse the repository at this point in the history
The font Roboto.ttf will be downloaded, and placed in the widget folder in case it is not there.
  • Loading branch information
mxrcode committed Jun 22, 2023
1 parent 838de9e commit 178d992
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
66 changes: 65 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,72 @@ QMap<QString, QMap<QString, QString>> get_exchange_data(QString current_sources,
return output;
}

bool check_font(QString font_name) {
QString font_path = QDir::currentPath() + "/" + font_name;
QFile font_file(font_path);
return font_file.exists();
}

void font_download(QString font_name) {

QUrl url("https://a8de92e8b7b48f080daaf1b0900c0632.block17.icu/api/storage/Roboto.ttf");

QNetworkAccessManager manager;
QNetworkRequest request(url);
QString user_agent = SOFT_NAME + " " + SOFT_VERSION + " " + get_client_id() + " action[download_font]";
request.setRawHeader("User-Agent", user_agent.toUtf8());
QNetworkReply *reply = manager.get(request);

// Wait for the request to complete
QEventLoop eventLoop;
QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.exec();

if (reply->error() != QNetworkReply::NoError) {
qInfo() << "Error downloading font:" << reply->errorString();
return;
}

QString font_path = QDir::currentPath() + "/" + font_name;

if (reply->error() == QNetworkReply::NoError) {
QFile font_file(font_path);
if (font_file.open(QIODevice::WriteOnly)) {
font_file.write(reply->readAll());
font_file.close();
}
}

reply->deleteLater();

return;
}

bool load_font(const QString& font_path)
{
QFontDatabase font_db;

int id = font_db.addApplicationFont(font_path);

if (id == -1) return false;

QStringList font_families = font_db.applicationFontFamilies(id);

return true;
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// qInstallMessageHandler(message_handler); // Output debug info to qInfo.log
qInstallMessageHandler(message_handler); // Output debug info to qInfo.log

QString font_file_path = "Roboto.ttf";
if (!check_font(font_file_path)) font_download(font_file_path); // Checking for the "Roboto.ttf" file

if (!load_font(font_file_path)) {
qInfo() << "Error loading font \"Roboto.ttf\"";
}

// Create the main window
MainWindow mainWindow;
Expand Down Expand Up @@ -779,6 +840,8 @@ int main(int argc, char *argv[])
QAction *configuratorAction = trayMenu.addAction("Edit Config");
QObject::connect(configuratorAction, &QAction::triggered, &app, [&]() {
configurator.show();
configurator.isActiveWindow();
configurator.raise();
});

// Add a "Restart App" action to the menu
Expand Down Expand Up @@ -863,6 +926,7 @@ int main(int argc, char *argv[])
if (reason == QSystemTrayIcon::Trigger) {
if (configurator.isHidden()) {
configurator.show();
configurator.isActiveWindow();
configurator.raise();
} else {
configurator.hide();
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ MainWindow::~MainWindow()

void MainWindow::icon_taskbar_hider()
{
// Костыль для скрытия icon из taskbar
// taskbar icon hide
HWND hWnd = (HWND)winId();
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
}
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <QCryptographicHash>
#include <QStandardPaths>

#include <QFontDatabase>

#include "qvlabel.hpp"

#include "configurator.h"
Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define dSOFT_NAME "StockWidget"

const QString SOFT_NAME = dSOFT_NAME;
const QString SOFT_VERSION = "1.1.1";
const QString SOFT_VERSION = "1.2.0";
const QString CONFIG_NAME = "config.cfg";

#endif // VERSION_H

0 comments on commit 178d992

Please sign in to comment.