1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * This file contains minor utility functions
- */
- #ifndef UTILS_H
- #define UTILS_H
- #include <QNetworkAccessManager>
- #include <QNetworkReply>
- #include <QEventLoop>
- #include <QFile>
- #include <QTimer>
- #include <QLockFile>
- #include <QDir>
- #include <QMessageBox>
- bool checkInternetConnection(QUrl url) {
- QNetworkAccessManager nam;
- QNetworkRequest req(url);
- QNetworkReply* reply = nam.get(req);
- QEventLoop loop;
- QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
- QTimer request_timeout_timer;
- QObject::connect(&request_timeout_timer, &QTimer::timeout, &loop, &QEventLoop::quit);
- request_timeout_timer.start(5000); // 5 seconds connection timeout
- loop.exec();
- return reply->bytesAvailable();
- }
- void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
- {
- QString message_type;
- switch (type) {
- case QtDebugMsg:
- message_type = "[DEBG]";
- break;
- case QtInfoMsg:
- message_type = "[INFO]";
- break;
- case QtWarningMsg:
- message_type = "[WARN]";
- break;
- case QtCriticalMsg:
- message_type = "[CRIT]";
- break;
- case QtFatalMsg:
- message_type = "[FATL]";
- break;
- default:
- message_type = "[UNKN]";
- break;
- }
- QString filename = context.category;
- QString function = context.function;
- QString line_number = QString::number(context.line);
- QString s = QString("%1: %2 ||| %3:%4 %5\n")
- .arg(message_type)
- .arg(msg)
- .arg(filename)
- .arg(line_number)
- .arg(function);
- #ifdef QT_DEBUG
- QFile log_file("legacy_v2.log");
- log_file.open(QIODevice::ReadWrite | QIODevice::Append);
- QTextStream stream(&log_file);
- stream << s;
- log_file.close();
- #endif
- fprintf(stderr, "%s", qUtf8Printable(s));
- fflush(stderr);
- }
- bool checkSingleAppInstance() {
- QLockFile lockFile(QDir::temp().absoluteFilePath("rulotro.lock"));
- if(!lockFile.tryLock(1)){
- qDebug() << "Lock file already exists! Some other application is already running...";
- QMessageBox msgBox;
- msgBox.setIcon(QMessageBox::Warning);
- msgBox.setText("Приложение уже запущено.\nРазрешено запускать только один экземпляр приложения.");
- msgBox.exec();
- return false;
- }
- return true;
- }
- #endif // UTILS_H
|