2021-04-05 06:50:58 -07:00
# ifndef FUNCTIONS_H
# define FUNCTIONS_H
# include <QFile>
# include <QString>
# include <QTextStream>
# include <fstream>
# include <QDir>
# include <QProcess>
# include <regex>
2021-06-23 05:16:04 -07:00
# include <QThread>
2021-07-14 15:23:54 -07:00
# include <QTimer>
2021-12-25 10:50:30 -08:00
# include <QDebug>
2022-04-03 17:23:49 -07:00
# include <QRandomGenerator>
2022-04-04 21:47:15 -07:00
# include <QDateTime>
2022-06-18 06:26:52 -07:00
# include <QDirIterator>
# include <QJsonDocument>
# include <QJsonParseError>
# include <QJsonObject>
# include <QJsonValue>
# include <QJsonArray>
2022-06-28 23:15:13 -07:00
# include <QCryptographicHash>
2023-05-22 11:11:12 -07:00
# include <QNetworkInterface>
2023-07-24 12:15:56 -07:00
# include <QMutex>
2021-04-05 06:50:58 -07:00
2021-06-25 19:09:42 -07:00
# include <stdio.h>
# include <fcntl.h>
# include <sys/ioctl.h>
2022-02-12 20:54:46 -08:00
# include <unistd.h>
2021-06-25 19:09:42 -07:00
2021-04-22 07:10:47 -07:00
// WoW, global variables and namespaces are awesome
namespace global {
2021-04-22 04:38:54 -07:00
namespace battery {
2021-04-22 07:10:47 -07:00
inline bool showLowBatteryDialog ;
inline bool showCriticalBatteryAlert ;
inline bool batteryAlertLock ;
2021-04-22 04:38:54 -07:00
}
namespace reader {
2021-09-04 20:46:47 -07:00
inline QString bookFile ;
2021-04-22 07:10:47 -07:00
inline int pageNumber ;
inline int bookNumber ;
inline bool skipOpenDialog ;
2021-04-23 16:22:53 -07:00
inline bool startBatteryWatchdog ;
2021-06-29 09:26:30 -07:00
inline bool startUsbmsPrompt ;
2021-06-17 05:34:00 -07:00
inline bool bookIsEpub ;
2021-08-19 09:55:12 -07:00
inline bool globalReadingSettings ;
2022-04-16 16:40:30 -07:00
inline int pdfOrientation ;
2022-08-08 16:57:45 -07:00
inline bool highlightAlreadyDone ;
2022-09-07 15:31:53 -07:00
inline int textAlignment ;
inline int lineSpacing ;
inline QString font ;
static inline int initialFontSize = 6 ;
inline int fontSize ;
inline int margins ;
2022-10-12 19:36:47 -07:00
inline QString currentViewportText ;
2021-04-22 07:10:47 -07:00
}
2021-04-30 20:55:38 -07:00
namespace kobox {
inline bool showKoboxSplash ;
2021-05-01 12:53:52 -07:00
inline bool koboxSettingsRebootDialog ;
2021-06-14 11:31:49 -07:00
inline bool resetKoboxUserDataBool ;
inline bool resetKoboxDialog ;
2021-04-30 20:55:38 -07:00
}
2021-04-22 07:10:47 -07:00
namespace mainwindow {
2021-04-23 19:23:24 -07:00
namespace tabSwitcher {
2021-04-24 06:34:35 -07:00
inline bool repaint ;
2022-06-28 23:15:13 -07:00
inline bool homePageWidgetCreated ;
2021-04-23 19:23:24 -07:00
inline bool appsWidgetCreated ;
inline bool appsWidgetSelected ;
2021-04-24 09:44:57 -07:00
inline bool settingsChooserWidgetCreated ;
inline bool settingsChooserWidgetSelected ;
2021-12-26 05:36:29 -08:00
inline bool libraryWidgetCreated ;
inline bool libraryWidgetSelected ;
2022-06-27 15:14:20 -07:00
inline bool localLibraryWidgetCreated ;
inline bool localLibraryWidgetSelected ;
2021-04-23 19:23:24 -07:00
}
2021-04-22 07:10:47 -07:00
inline bool updateDialog ;
inline bool lowBatteryDialog ;
2021-05-09 11:07:05 -07:00
}
namespace usbms {
2021-05-07 22:03:25 -07:00
inline bool usbmsDialog ;
2021-05-09 11:07:05 -07:00
inline bool showUsbmsDialog ;
inline bool launchUsbms ;
2021-05-13 18:40:05 -07:00
inline bool koboxExportExtensions ;
2021-04-22 04:38:54 -07:00
}
2021-05-01 12:53:52 -07:00
namespace settings {
inline bool settingsRebootDialog ;
}
2021-06-08 04:49:52 -07:00
namespace text {
inline bool textBrowserDialog ;
inline QString textBrowserContents ;
2023-07-24 12:15:56 -07:00
inline QString textBrowserTitle = " " ; // At default: empty; "Information" will be displayed
2021-06-08 04:49:52 -07:00
}
2021-07-05 10:45:35 -07:00
namespace keyboard {
inline bool keyboardDialog ;
inline bool keypadDialog ;
2021-07-05 19:21:20 -07:00
inline bool searchDialog ;
2021-10-08 20:37:32 -07:00
inline bool encfsDialog ;
2021-07-05 21:13:11 -07:00
inline bool vncDialog ;
2021-07-14 15:23:54 -07:00
inline bool wifiPassphraseDialog ;
2024-01-13 11:08:37 -08:00
inline bool telemetryMessageDialog ;
2021-07-05 10:45:35 -07:00
inline QString keyboardText ;
inline QString keypadText ;
2022-09-13 05:20:37 -07:00
inline bool embed = true ;
2021-07-05 10:45:35 -07:00
}
2021-07-13 22:24:30 -07:00
namespace toast {
inline QString message ;
2021-07-14 15:23:54 -07:00
inline bool modalToast ;
inline bool indefiniteToast ;
2021-09-05 06:37:07 -07:00
inline int delay ;
2021-07-13 22:24:30 -07:00
}
2021-07-16 13:41:36 -07:00
namespace device {
inline bool isWifiAble ;
}
2021-08-19 05:23:18 -07:00
namespace otaUpdate {
inline bool isUpdateOta ;
inline bool downloadOta ;
}
2021-10-08 20:37:32 -07:00
namespace encfs {
inline QString passphrase ;
2021-10-10 12:02:20 -07:00
inline QString unlockTime ;
2022-04-04 22:37:04 -07:00
inline QString lockdownMessage ;
2021-10-10 12:02:20 -07:00
inline bool cancelSetup ;
inline bool cancelUnlock ;
inline bool lockdown ;
2021-10-18 05:46:27 -07:00
inline bool enableStorageEncryptionDialog ;
2021-10-17 19:31:44 -07:00
inline bool disableStorageEncryptionDialog ;
2021-11-14 18:22:58 -08:00
inline bool errorNoBooksInDropboxDialog ;
2021-12-31 12:50:59 -08:00
inline bool repackDialog ;
2021-10-08 20:37:32 -07:00
}
2021-12-27 12:04:39 -08:00
namespace library {
inline unsigned long bookId ;
inline bool isLatestBook ;
inline int latestBookNumber ;
inline QString bookTitle ;
inline bool librarySearchDialog ;
2021-12-27 20:59:28 -08:00
inline bool libraryResults ;
2023-05-21 10:19:18 -07:00
inline bool librarySyncDialog ;
2021-12-27 12:04:39 -08:00
}
2022-07-12 09:34:36 -07:00
namespace bookInfoDialog {
inline bool localInfoDialog ;
}
2022-06-27 15:14:20 -07:00
namespace localLibrary {
static inline QString rawDatabasePath = " /inkbox/LocalLibrary.db.raw " ;
2022-07-06 11:24:23 -07:00
static inline QString databaseDirectoryPath = " /mnt/onboard/onboard/.database/ " ;
static inline QString databasePath = databaseDirectoryPath + " LocalLibrary.db " ;
static inline QString recentBooksDatabasePath = databaseDirectoryPath + " RecentBooks.db " ;
static inline QString pinnedBooksDatabasePath = databaseDirectoryPath + " PinnedBooks.db " ;
2022-08-08 16:57:45 -07:00
static inline QString highlightsDatabasePath = databaseDirectoryPath + " Highlights.db " ;
2022-09-13 05:20:37 -07:00
static inline QString todoDatabasePath = databaseDirectoryPath + " ToDo.db " ;
2022-09-18 20:59:46 -07:00
// Maximum signed integer value for 32-bit systems
static inline int folderID = 2147483647 ;
2022-06-30 06:52:48 -07:00
inline bool headless ;
2022-07-06 11:24:23 -07:00
namespace bookOptionsDialog {
inline int bookID ;
2022-07-17 23:40:32 -07:00
inline bool deleteOption = true ;
2022-07-06 11:24:23 -07:00
inline bool bookDeleted ;
2022-07-17 23:40:32 -07:00
inline bool bookPinAction ;
2022-09-18 20:59:46 -07:00
inline QString folderPath ;
inline bool isFolder = false ;
2022-07-06 11:24:23 -07:00
}
2022-06-27 15:14:20 -07:00
}
2022-03-07 11:15:54 -08:00
namespace localStorage {
inline QStringList searchResultsPaths ;
}
2022-04-04 21:47:15 -07:00
namespace logger {
inline bool status ;
}
2022-06-17 20:59:21 -07:00
namespace userApps {
2022-06-28 23:15:13 -07:00
inline bool appCompatibilityDialog ;
inline QString appCompatibilityText ;
inline bool appCompatibilityLastContinueStatus = true ; // This is for RequiredFeatures to show only one dialog if 'Cancel' is clicked.
inline bool appInfoDialog ;
inline bool launchApp ;
}
namespace homePageWidget {
2022-06-30 06:52:48 -07:00
static inline int recentBooksNumber = 8 ;
static inline int recentBooksNumberPerRow = 4 ;
static inline int recentBooksRowNumber = global : : homePageWidget : : recentBooksNumber / global : : homePageWidget : : recentBooksNumberPerRow ;
2022-07-17 23:40:32 -07:00
static inline int pinnedBooksNumber = 4 ;
static inline int pinnedBooksNumberPerRow = 4 ;
static inline int pinnedBooksRowNumber = global : : homePageWidget : : pinnedBooksNumber / global : : homePageWidget : : pinnedBooksNumberPerRow ;
2022-06-17 20:59:21 -07:00
}
2022-08-09 19:31:29 -07:00
namespace highlightsListDialog {
inline QString bookPath ;
}
2022-08-15 12:10:32 -07:00
namespace wifi {
2022-08-22 13:09:18 -07:00
enum class wifiState {
2022-08-22 06:42:39 -07:00
configured ,
enabled ,
disabled ,
unknown , // To not confuse lastWifiState
2022-08-15 12:10:32 -07:00
} ;
inline bool isConnected ;
2022-08-16 06:25:01 -07:00
class wifiNetworkData {
public :
QString mac ;
QString name ;
bool encryption ;
int signal ;
} ;
2022-08-15 12:10:32 -07:00
}
2023-07-24 12:15:56 -07:00
namespace audio {
2024-01-09 07:18:12 -08:00
inline bool enabled = false ;
struct musicFile {
QString path ;
QString name ; // Cut path for easier use in names
int lengths ; // length Seconds
QString length ; // In minutes:seconds
int id ;
} ;
// 'None' is when 'currentAction' is empty
enum class Action { // Function will be called with this enum
Play ,
Next ,
Previous ,
Pause ,
Continue ,
Stop , // Sets 'paused' to false, 'isSomethingCurrentlyPlaying' to false, and 'itemCurrentlyPlaying' to -1; also stops playing
SetVolume ,
} ;
inline QVector < Action > currentAction ;
inline QVector < musicFile > queue ;
inline QVector < musicFile > fileList ;
inline int itemCurrentlyPlaying = - 1 ; // Also indicates in the queue menu which a gray color which is playing
inline QMutex audioMutex ; // These variables will be shared between threads, so here, it's to protect it
inline int progressSeconds = - 5 ; // -5 at default to avoid cutting song too early... yea
inline bool paused = false ;
inline bool isSomethingCurrentlyPlaying = false ; // Pause and continue
inline bool firstScan = true ;
inline int volumeLevel = 40 ; // Default save value
inline bool songChanged = false ;
}
namespace telemetry {
inline bool enabled = false ;
inline bool telemetryDialog = false ;
}
2021-06-08 05:03:46 -07:00
inline QString systemInfoText ;
2021-07-05 19:21:20 -07:00
inline bool forbidOpenSearchDialog ;
2022-05-22 12:38:02 -07:00
inline bool isN705 = false ;
inline bool isN905C = false ;
inline bool isN613 = false ;
inline bool isN873 = false ;
inline bool isN236 = false ;
inline bool isN437 = false ;
inline bool isN306 = false ;
2023-07-20 12:11:12 -07:00
inline bool isN249 = false ;
2022-05-22 12:38:02 -07:00
inline bool isKT = false ;
2021-09-05 08:47:09 -07:00
inline bool runningInstanceIsReaderOnly ;
2022-04-02 13:49:18 -07:00
inline QString deviceID ;
2021-04-22 04:38:54 -07:00
}
2021-04-05 06:50:58 -07:00
// https://stackoverflow.com/questions/6080853/c-multiple-definition-error-for-global-functions-in-the-header-file/20679534#20679534
namespace {
2021-05-26 05:33:40 -07:00
QString deviceUID ;
2021-06-25 19:09:42 -07:00
QString device ;
2023-07-20 17:58:26 -07:00
QString batteryLevel ;
2021-06-14 09:24:20 -07:00
QString kernelVersion ;
2023-07-20 17:58:26 -07:00
int batteryLevelInt ;
2021-06-20 19:57:10 -07:00
int defaultEpubPageWidth ;
int defaultEpubPageHeight ;
2021-08-29 12:05:07 -07:00
int defaultPdfPageWidth ;
int defaultPdfPageHeight ;
2021-04-05 06:50:58 -07:00
bool checked_box = false ;
2022-04-16 20:20:24 -07:00
QFile logFile ( " /external_root/var/log/inkbox-gui.log " ) ;
2022-07-12 09:34:36 -07:00
void log ( QString message , QString className = " undefined " , bool applicationStart = false ) {
2022-04-04 21:47:15 -07:00
if ( global : : logger : : status = = true ) {
2022-04-16 20:20:24 -07:00
QString initialTime ;
if ( applicationStart = = true ) {
initialTime = QDateTime : : currentDateTime ( ) . toString ( " dd/MM/yyyy @ hh:mm:ss " ) ;
}
2022-04-04 21:47:15 -07:00
QDebug logger = qDebug ( ) ;
logger . noquote ( ) ;
2022-04-16 20:20:24 -07:00
QStringList logStringList ;
logStringList < < QDateTime : : currentDateTime ( ) . toString ( " dd/MM/yyyy @ hh:mm:ss " ) < < " | " < < className + " : " < < message . trimmed ( ) ;
QString logString = logStringList . join ( " " ) ;
logger < < logString ;
if ( ! logFile . isOpen ( ) ) {
logFile . open ( QIODevice : : Append | QIODevice : : Text ) ;
}
QTextStream logFileOut ( & logFile ) ;
if ( applicationStart = = true ) {
logFileOut < < " ========== InkBox binary start at " < < initialTime < < " ========== " < < Qt : : endl ;
}
logFileOut < < logString < < Qt : : endl ;
logFile . close ( ) ;
2022-04-04 21:47:15 -07:00
}
}
2022-07-26 18:08:25 -07:00
void logEnabled ( QString configOption , QString className ) {
log ( " Enabling " + configOption + " setting " , className ) ;
}
void logDisabled ( QString configOption , QString className ) {
log ( " Disabling " + configOption + " setting " , className ) ;
}
2021-04-05 06:50:58 -07:00
bool checkconfig ( QString file ) {
2021-09-05 06:49:37 -07:00
if ( QFile : : exists ( file ) ) {
QFile config ( file ) ;
config . open ( QIODevice : : ReadOnly ) ;
QTextStream in ( & config ) ;
const QString content = in . readAll ( ) ;
2023-06-13 14:42:14 -07:00
config . close ( ) ;
2023-07-24 12:15:56 -07:00
if ( content . contains ( " true " ) ) {
2021-09-05 06:49:37 -07:00
return true ;
}
else {
return false ;
}
2021-04-05 06:50:58 -07:00
}
else {
2023-07-24 12:15:56 -07:00
QString function = __func__ ; log ( function + " : Warning: File ' " + file + " ' doesn't exist, returning false " , " functions " ) ;
2021-04-05 06:50:58 -07:00
return false ;
}
return 0 ;
} ;
2021-06-26 16:29:51 -07:00
void setDefaultWorkDir ( ) {
QDir : : setCurrent ( " /mnt/onboard/.adds/inkbox " ) ;
}
2023-07-11 06:22:48 -07:00
int brightnessCheckconfig ( QString file ) {
2021-09-05 06:49:37 -07:00
if ( QFile : : exists ( file ) ) {
QFile config ( file ) ;
config . open ( QIODevice : : ReadWrite ) ;
QTextStream in ( & config ) ;
const QString content = in . readAll ( ) ;
2023-07-11 06:22:48 -07:00
int contentInt = content . toInt ( ) ;
return contentInt ;
2021-09-05 06:49:37 -07:00
config . close ( ) ;
}
else {
return EXIT_FAILURE ;
}
2021-04-05 06:50:58 -07:00
return 0 ;
}
2023-07-11 06:22:48 -07:00
void setBrightness ( int value ) {
2023-07-20 12:11:12 -07:00
if ( global : : deviceID = = " n249 \n " ) {
if ( QFile : : exists ( " /var/run/brightness_write " ) ) {
std : : ofstream fhandler ;
fhandler . open ( " /var/run/brightness_write " ) ;
fhandler < < value ;
fhandler . close ( ) ;
}
}
else {
if ( QFile : : exists ( " /var/run/brightness " ) ) {
std : : ofstream fhandler ;
fhandler . open ( " /var/run/brightness " ) ;
fhandler < < value ;
fhandler . close ( ) ;
}
2022-04-06 20:24:16 -07:00
}
2021-04-05 06:50:58 -07:00
}
2023-07-11 06:22:48 -07:00
void setBrightness_ntxio ( int value ) {
2021-06-25 19:09:42 -07:00
// Thanks to Kevin Short for this (GloLight)
int light ;
2021-06-27 10:28:41 -07:00
if ( ( light = open ( " /dev/ntx_io " , O_RDWR ) ) = = - 1 ) {
2023-07-11 06:22:48 -07:00
fprintf ( stderr , " Error opening ntx_io device \n " ) ;
2021-06-25 19:09:42 -07:00
}
ioctl ( light , 241 , value ) ;
2022-02-12 20:54:46 -08:00
close ( light ) ;
2021-06-25 19:09:42 -07:00
}
2023-07-11 06:22:48 -07:00
int displayQuote ( ) {
2022-04-03 17:23:49 -07:00
int quoteNumber = QRandomGenerator : : global ( ) - > bounded ( 1 , 6 ) ;
return quoteNumber ;
2021-04-05 06:50:58 -07:00
}
2022-08-10 17:49:47 -07:00
bool writeFile ( QString filename , QString content ) {
2022-06-25 17:12:50 -07:00
QFile file ( filename ) ;
2022-07-25 21:29:13 -07:00
if ( file . open ( QIODevice : : ReadWrite | QIODevice : : Truncate | QIODevice : : Text ) ) {
2022-06-25 17:12:50 -07:00
QTextStream stream ( & file ) ;
stream < < content ;
2022-08-10 17:49:47 -07:00
return true ;
2022-06-25 17:12:50 -07:00
}
else {
QString function = __func__ ; log ( function + " : Failed to write string ' " + content + " ' to file ' " + filename + " ' " , " functions " ) ;
2022-08-10 17:49:47 -07:00
return false ;
2022-06-25 17:12:50 -07:00
}
}
2021-12-26 16:08:21 -08:00
QString readFile ( QString file ) {
if ( QFile : : exists ( file ) ) {
QFile fileToRead ( file ) ;
fileToRead . open ( QIODevice : : ReadOnly ) ;
QTextStream in ( & fileToRead ) ;
QString content = in . readAll ( ) ;
return content ;
}
else {
return NULL ;
}
}
2023-07-11 06:22:48 -07:00
void brightnessWriteconfig ( int value ) {
2021-04-05 06:50:58 -07:00
std : : ofstream fhandler ;
fhandler . open ( " .config/03-brightness/config " ) ;
fhandler < < value ;
fhandler . close ( ) ;
}
2023-07-11 06:22:48 -07:00
void warmthWriteconfig ( int value ) {
2021-07-16 13:41:36 -07:00
std : : ofstream fhandler ;
fhandler . open ( " .config/03-brightness/config-warmth " ) ;
fhandler < < value ;
fhandler . close ( ) ;
}
2023-07-11 06:22:48 -07:00
int getBrightness ( ) {
2022-04-02 13:49:18 -07:00
if ( global : : deviceID = = " n613 \n " ) {
2023-07-11 06:22:48 -07:00
QString brightnessConfig = readFile ( " .config/03-brightness/config " ) ;
2021-06-28 12:26:04 -07:00
int brightness ;
2023-07-11 06:22:48 -07:00
if ( brightnessConfig . isEmpty ( ) ) {
2021-06-28 12:26:04 -07:00
brightness = 0 ;
}
else {
2023-07-11 06:22:48 -07:00
brightness = brightnessConfig . toInt ( ) ;
2021-06-28 12:26:04 -07:00
}
return brightness ;
}
else {
2022-04-06 20:24:16 -07:00
if ( QFile : : exists ( " /var/run/brightness " ) ) {
QFile brightness ( " /var/run/brightness " ) ;
brightness . open ( QIODevice : : ReadOnly ) ;
QString valuestr = brightness . readAll ( ) ;
int value = valuestr . toInt ( ) ;
brightness . close ( ) ;
return value ;
}
else {
return 0 ;
}
2021-06-28 12:26:04 -07:00
}
return 0 ;
}
2023-10-20 12:19:38 -07:00
QString getPowerSupplyOfType ( QString type ) {
QDirIterator supplies ( " /sys/class/power_supply " , QDirIterator : : NoIteratorFlags ) ;
while ( supplies . hasNext ( ) ) {
supplies . next ( ) ;
QString supply = supplies . filePath ( ) ;
2023-12-26 18:22:46 -08:00
// Badly-classified device by some ntx kernels, ignore
2023-10-20 12:19:38 -07:00
if ( ( type = = " Battery " ) & &
( supplies . fileName ( ) = = " mc13892_charger " ) ) {
continue ;
}
if ( readFile ( supplies . filePath ( ) + " /type " ) . trimmed ( ) = = type ) {
return supply ;
}
}
return NULL ;
}
QString getBatteryFile ( ) {
static QString capacity ;
if ( capacity . isNull ( ) ) {
QString battery = getPowerSupplyOfType ( " Battery " ) ;
if ( ! battery . isNull ( ) ) {
capacity = battery + " /capacity " ;
}
}
return capacity ;
}
2023-07-11 06:22:48 -07:00
void getBatteryLevel ( ) {
2023-07-20 17:58:26 -07:00
batteryLevelInt = 100 ;
batteryLevel = " 100% " ;
2022-05-23 12:24:21 -07:00
if ( global : : deviceID = = " kt \n " ) {
2023-07-20 17:58:26 -07:00
if ( QFile : : exists ( " /sys/devices/system/yoshi_battery/yoshi_battery0/battery_capacity " ) ) {
batteryLevel = readFile ( " /sys/devices/system/yoshi_battery/yoshi_battery0/battery_capacity " ) . trimmed ( ) ;
batteryLevelInt = batteryLevel . toInt ( ) ;
batteryLevel . append ( " % " ) ;
2022-05-23 12:24:21 -07:00
}
2021-12-25 07:36:38 -08:00
}
2023-07-20 17:58:26 -07:00
else {
2023-10-20 12:19:38 -07:00
// probably superfluous, catched by default
2023-07-20 17:58:26 -07:00
if ( QFile : : exists ( " /sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity " ) ) {
batteryLevel = readFile ( " /sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity " ) . trimmed ( ) ;
batteryLevelInt = batteryLevel . toInt ( ) ;
batteryLevel . append ( " % " ) ;
2022-05-23 12:24:21 -07:00
}
2023-07-24 11:05:22 -07:00
else {
2023-10-20 12:19:38 -07:00
QString path = getBatteryFile ( ) ;
2023-07-24 11:05:22 -07:00
if ( QFile : : exists ( path ) ) {
batteryLevel = readFile ( path ) . trimmed ( ) ;
batteryLevelInt = batteryLevel . toInt ( ) ;
batteryLevel . append ( " % " ) ;
}
}
2021-12-25 07:36:38 -08:00
}
2021-04-05 06:50:58 -07:00
}
void writeconfig ( std : : string file , std : : string config ) {
2021-09-05 06:49:37 -07:00
std : : ofstream fhandler ;
fhandler . open ( file ) ;
fhandler < < config < < std : : boolalpha < < checked_box < < std : : endl ;
fhandler . close ( ) ;
2021-04-05 06:50:58 -07:00
}
bool checkconfig_match ( QString file , std : : string pattern ) {
QFile config ( file ) ;
config . open ( QIODevice : : ReadWrite ) ;
QTextStream in ( & config ) ;
const QString content = in . readAll ( ) ;
std : : string contentstr = content . toStdString ( ) ;
2023-06-13 14:42:14 -07:00
config . close ( ) ;
2021-04-05 06:50:58 -07:00
// Thanks to https://stackoverflow.com/questions/22516463/how-do-i-find-a-complete-word-not-part-of-it-in-a-string-in-c
std : : regex r ( " \\ b " + pattern + " \\ b " ) ;
std : : smatch m ;
if ( std : : regex_search ( contentstr , m , r ) ) {
return true ;
}
else {
return false ;
}
return 0 ;
} ;
2021-04-22 04:38:54 -07:00
bool isBatteryLow ( ) {
// Checks if battery level is under 15% of total capacity.
2023-07-11 06:22:48 -07:00
getBatteryLevel ( ) ;
2023-07-20 17:58:26 -07:00
if ( batteryLevelInt < = 15 ) {
2021-04-22 04:38:54 -07:00
return true ;
}
else {
return false ;
}
return 0 ;
}
bool isBatteryCritical ( ) {
// Checks if the battery level is critical (i.e. <= 5%)
2023-07-11 06:22:48 -07:00
getBatteryLevel ( ) ;
2023-07-20 17:58:26 -07:00
if ( batteryLevelInt < = 5 ) {
2022-04-06 20:24:16 -07:00
QString function = __func__ ; log ( function + " : Battery is at a critical charge level! " , " functions " ) ;
2021-04-22 04:38:54 -07:00
return true ;
}
else {
return false ;
}
return 0 ;
}
2021-06-27 10:28:41 -07:00
void zeroBrightness ( ) {
2022-04-02 13:49:18 -07:00
if ( global : : deviceID ! = " n613 \n " ) {
2023-07-11 06:22:48 -07:00
setBrightness ( 0 ) ;
2021-06-27 10:28:41 -07:00
}
else {
2023-07-11 06:22:48 -07:00
setBrightness_ntxio ( 0 ) ;
2021-06-27 10:28:41 -07:00
}
}
2021-04-22 04:38:54 -07:00
void poweroff ( bool splash ) {
2022-04-10 18:37:33 -07:00
log ( " Powering off " , " functions " ) ;
2021-04-22 04:38:54 -07:00
if ( splash = = true ) {
2021-05-28 05:36:24 -07:00
QString prog ( " /sbin/poweroff " ) ;
2021-04-22 04:38:54 -07:00
QStringList args ;
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
2021-08-29 12:05:07 -07:00
proc - > waitForFinished ( ) ;
proc - > deleteLater ( ) ;
2021-04-22 04:38:54 -07:00
}
else {
2021-05-28 05:36:24 -07:00
QString prog ( " /sbin/poweroff " ) ;
2021-04-22 04:38:54 -07:00
QStringList args ;
2021-05-28 05:36:24 -07:00
args < < " no_splash " ;
2021-04-22 04:38:54 -07:00
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
2021-08-29 12:05:07 -07:00
proc - > waitForFinished ( ) ;
proc - > deleteLater ( ) ;
2021-04-22 04:38:54 -07:00
}
}
void reboot ( bool splash ) {
2022-04-10 18:37:33 -07:00
log ( " Rebooting " , " functions " ) ;
2021-04-22 04:38:54 -07:00
if ( splash = = true ) {
2021-06-20 10:31:24 -07:00
QString prog ( " /sbin/reboot " ) ;
2021-04-22 04:38:54 -07:00
QStringList args ;
2021-06-14 11:31:49 -07:00
if ( global : : kobox : : resetKoboxUserDataBool = = true ) {
args < < " splash " < < " reset_kobox " ;
}
2021-04-22 04:38:54 -07:00
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
2021-08-29 12:05:07 -07:00
proc - > waitForFinished ( ) ;
proc - > deleteLater ( ) ;
2021-04-22 04:38:54 -07:00
}
else {
2021-06-20 10:31:24 -07:00
QString prog ( " /sbin/reboot " ) ;
2021-04-22 04:38:54 -07:00
QStringList args ;
2021-06-14 11:31:49 -07:00
if ( global : : kobox : : resetKoboxUserDataBool = = true ) {
args < < " no_splash " < < " reset_kobox " ;
}
else {
args < < " no_splash " ;
}
2021-04-22 04:38:54 -07:00
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
2021-08-29 12:05:07 -07:00
proc - > waitForFinished ( ) ;
proc - > deleteLater ( ) ;
2021-04-22 04:38:54 -07:00
}
}
2024-01-13 11:08:37 -08:00
QString getUID ( ) {
2021-05-26 05:06:50 -07:00
QString prog ( " dd " ) ;
QStringList args ;
args < < " if=/dev/mmcblk0 " < < " bs=512 " < < " skip=1 " < < " count=1 " < < " status=none " ;
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
proc - > waitForFinished ( ) ;
2021-05-26 05:33:40 -07:00
deviceUID = proc - > readAllStandardOutput ( ) ;
deviceUID = deviceUID . left ( 256 ) ;
2021-08-29 12:05:07 -07:00
proc - > deleteLater ( ) ;
2024-01-13 11:08:37 -08:00
return deviceUID ;
2021-05-26 05:06:50 -07:00
}
2021-06-14 09:24:20 -07:00
void getKernelVersion ( ) {
QString prog ( " uname " ) ;
QStringList args ;
args < < " -r " ;
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
proc - > waitForFinished ( ) ;
kernelVersion = proc - > readAllStandardOutput ( ) ;
2021-06-23 05:16:04 -07:00
kernelVersion = kernelVersion . trimmed ( ) ;
2021-06-26 16:29:51 -07:00
2022-02-15 19:47:10 -08:00
proc - > deleteLater ( ) ;
2021-06-26 16:29:51 -07:00
setDefaultWorkDir ( ) ;
2023-07-11 06:22:48 -07:00
writeFile ( " /external_root/run/initrd-fifo " , " get_kernel_build_id \n " ) ;
2022-02-20 17:03:40 -08:00
QThread : : msleep ( 100 ) ;
2023-07-11 06:22:48 -07:00
writeFile ( " /external_root/run/initrd-fifo " , " get_kernel_commit \n " ) ;
2021-06-23 05:16:04 -07:00
QThread : : msleep ( 100 ) ;
2021-06-26 16:29:51 -07:00
2023-07-11 06:22:48 -07:00
QString kernelBuildID = readFile ( " /external_root/run/kernel_build_id " ) . trimmed ( ) ;
2021-06-23 05:16:04 -07:00
kernelVersion . append ( " , build " ) ;
kernelVersion . append ( kernelBuildID ) ;
2022-02-20 17:03:40 -08:00
2023-07-11 06:22:48 -07:00
QString kernelCommit = readFile ( " /external_root/run/kernel_commit " ) . trimmed ( ) ;
2022-02-20 17:03:40 -08:00
kernelVersion . append ( " , commit " ) ;
kernelVersion . append ( kernelCommit ) ;
2021-06-14 09:24:20 -07:00
}
2021-07-20 20:27:32 -07:00
QString getConnectionInformation ( ) {
QString getIpProg ( " sh " ) ;
QStringList getIpArgs ;
2023-07-20 12:11:12 -07:00
if ( global : : deviceID ! = " n437 \n " and global : : deviceID ! = " n249 \n " ) {
2022-02-18 08:06:09 -08:00
getIpArgs < < " -c " < < " /sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}' " ;
}
else {
getIpArgs < < " -c " < < " /sbin/ifconfig wlan0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}' " ;
}
2021-07-20 20:27:32 -07:00
QProcess * getIpProc = new QProcess ( ) ;
getIpProc - > start ( getIpProg , getIpArgs ) ;
getIpProc - > waitForFinished ( ) ;
QString ipAddress = getIpProc - > readAllStandardOutput ( ) ;
2021-08-18 17:11:18 -07:00
if ( ipAddress = = " " ) {
ipAddress = " Not available " ;
}
2021-08-29 12:05:07 -07:00
getIpProc - > deleteLater ( ) ;
2021-07-20 20:27:32 -07:00
return ipAddress ;
}
2021-06-08 05:03:46 -07:00
void getSystemInfo ( ) {
getUID ( ) ;
2021-06-14 09:24:20 -07:00
getKernelVersion ( ) ;
global : : systemInfoText = " <b>InkBox OS version " ;
2023-07-11 06:22:48 -07:00
global : : systemInfoText . append ( readFile ( " /external_root/opt/isa/version " ) ) ;
2024-01-01 07:30:05 -08:00
global : : systemInfoText . append ( " </b><br>Copyright <font face='Inter'>©</font> 2021-2024 Nicolas Mailloux and contributors<br>Special thanks to: Szybet, NiLuJe, akemnade, Rain92 (GitHub) " ) ;
2023-06-12 09:31:09 -07:00
global : : systemInfoText . append ( " <br><b>GUI Git commit:</b> " ) ;
2022-02-20 21:01:03 -08:00
global : : systemInfoText . append ( GIT_VERSION ) ;
2021-06-20 14:32:25 -07:00
global : : systemInfoText . append ( " <br><b>Device UID:</b> " ) ;
2021-06-08 05:03:46 -07:00
global : : systemInfoText . append ( deviceUID ) ;
2021-06-20 14:32:25 -07:00
global : : systemInfoText . append ( " <br><b>Kernel version:</b> " ) ;
2021-06-14 09:24:20 -07:00
global : : systemInfoText . append ( kernelVersion ) ;
2021-06-23 05:16:04 -07:00
global : : systemInfoText . append ( " <br><b>Device:</b> " ) ;
2022-04-02 13:49:18 -07:00
QString device = global : : deviceID . trimmed ( ) ;
2021-06-23 05:16:04 -07:00
global : : systemInfoText . append ( device ) ;
2021-07-20 20:27:32 -07:00
QString ipAddress = getConnectionInformation ( ) ;
global : : systemInfoText . append ( " <br><b>IP address: </b> " ) ;
global : : systemInfoText . append ( ipAddress ) ;
2021-06-08 05:03:46 -07:00
}
2021-06-14 11:31:49 -07:00
void resetKoboxUserData ( ) {
2022-04-10 18:37:33 -07:00
log ( " Resetting KoBox user data " , " functions " ) ;
2021-06-14 11:31:49 -07:00
global : : kobox : : resetKoboxUserDataBool = true ;
reboot ( true ) ;
}
2021-06-20 10:31:24 -07:00
QString findEpubMetadata ( QString book_file , QString metadata ) {
2022-04-10 18:37:33 -07:00
log ( " Finding ePUB metadata, query: " + metadata , " functions " ) ;
2021-06-20 10:31:24 -07:00
setDefaultWorkDir ( ) ;
QString prog ( " sh " ) ;
QStringList args ;
args < < " find_epub_metadata.sh " < < book_file < < metadata ;
QProcess * proc = new QProcess ( ) ;
proc - > start ( prog , args ) ;
proc - > waitForFinished ( ) ;
QString returnedMetadata = proc - > readAllStandardOutput ( ) ;
2022-04-06 20:24:16 -07:00
QString function = __func__ ; log ( function + " : ePUB metadata is: " + returnedMetadata , " functions " ) ;
2021-06-20 10:31:24 -07:00
return returnedMetadata ;
}
2021-08-29 12:05:07 -07:00
void defineDefaultPageSize ( int fileType ) {
/* fileType can be:
* 0 : ePUB
* 1 : PDF
*/
if ( fileType = = 0 ) {
2022-04-02 13:49:18 -07:00
if ( global : : deviceID = = " n705 \n " ) {
2021-08-29 12:05:07 -07:00
defaultEpubPageHeight = 365 ;
defaultEpubPageWidth = 365 ;
}
2022-05-22 12:38:02 -07:00
else if ( global : : deviceID = = " n905 \n " or global : : deviceID = = " kt \n " ) {
2021-08-29 12:05:07 -07:00
defaultEpubPageHeight = 425 ;
defaultEpubPageWidth = 425 ;
}
2023-07-20 12:11:12 -07:00
else if ( global : : deviceID = = " n613 \n " or global : : deviceID = = " n236 \n " or global : : deviceID = = " n437 \n " or global : : deviceID = = " n306 \n " or global : : deviceID = = " n249 \n " or global : : deviceID = = " emu \n " ) {
2021-08-29 12:05:07 -07:00
defaultEpubPageHeight = 450 ;
defaultEpubPageWidth = 450 ;
}
2022-04-02 13:49:18 -07:00
else if ( global : : deviceID = = " n873 \n " ) {
2021-08-29 12:05:07 -07:00
defaultEpubPageHeight = 525 ;
defaultEpubPageWidth = 525 ;
}
2022-04-06 20:24:16 -07:00
QString function = __func__ ;
log ( function + " : Defined default ePUB page height to " + QString : : number ( defaultEpubPageHeight ) , " functions " ) ;
log ( function + " : Defined default ePUB page width to " + QString : : number ( defaultEpubPageWidth ) , " functions " ) ;
2021-06-26 07:21:07 -07:00
}
2021-08-29 12:05:07 -07:00
else if ( fileType = = 1 ) {
2022-05-22 12:38:02 -07:00
if ( global : : deviceID = = " n705 \n " or global : : deviceID = = " n905 \n " or global : : deviceID = = " kt \n " ) {
2022-04-16 16:40:30 -07:00
if ( global : : reader : : pdfOrientation = = 0 ) {
defaultPdfPageHeight = 750 ;
defaultPdfPageWidth = 550 ;
}
else {
defaultPdfPageHeight = 550 ;
defaultPdfPageWidth = 750 ;
}
2021-08-29 12:05:07 -07:00
}
2022-04-03 17:23:49 -07:00
else if ( global : : deviceID = = " n613 \n " or global : : deviceID = = " n236 \n " or global : : deviceID = = " n306 \n " or global : : deviceID = = " emu \n " ) {
2022-04-16 16:40:30 -07:00
if ( global : : reader : : pdfOrientation = = 0 ) {
defaultPdfPageHeight = 974 ;
defaultPdfPageWidth = 708 ;
}
else {
defaultPdfPageHeight = 708 ;
defaultPdfPageWidth = 974 ;
}
2021-08-29 12:05:07 -07:00
}
2023-07-20 12:11:12 -07:00
else if ( global : : deviceID = = " n437 \n " or global : : deviceID = = " n249 \n " ) {
2022-04-16 16:40:30 -07:00
if ( global : : reader : : pdfOrientation = = 0 ) {
defaultPdfPageHeight = 1398 ;
defaultPdfPageWidth = 1022 ;
}
else {
defaultPdfPageHeight = 1022 ;
defaultPdfPageWidth = 1398 ;
}
2022-02-06 19:49:06 -08:00
}
2022-04-02 13:49:18 -07:00
else if ( global : : deviceID = = " n873 \n " ) {
2022-04-16 16:40:30 -07:00
if ( global : : reader : : pdfOrientation = = 0 ) {
defaultPdfPageHeight = 1630 ;
defaultPdfPageWidth = 1214 ;
}
else {
defaultPdfPageHeight = 1214 ;
defaultPdfPageWidth = 1630 ;
}
2021-08-29 12:05:07 -07:00
}
2022-04-06 20:24:16 -07:00
QString function = __func__ ;
log ( function + " Defined default PDF page height to " + QString : : number ( defaultPdfPageHeight ) , " functions " ) ;
log ( function + " Defined default PDF page width to " + QString : : number ( defaultPdfPageWidth ) , " functions " ) ;
2021-07-10 22:08:38 -07:00
}
2021-06-20 19:57:10 -07:00
}
2023-07-11 06:22:48 -07:00
void preSetBrightness ( int brightnessValue ) {
2023-07-20 12:11:12 -07:00
if ( global : : deviceID = = " n705 \n " or global : : deviceID = = " n905 \n " or global : : deviceID = = " n873 \n " or global : : deviceID = = " n236 \n " or global : : deviceID = = " n437 \n " or global : : deviceID = = " n306 \n " or global : : deviceID = = " n249 \n " or global : : deviceID = = " kt \n " ) {
2023-07-11 06:22:48 -07:00
setBrightness ( brightnessValue ) ;
2021-06-28 12:26:04 -07:00
}
2022-04-02 13:49:18 -07:00
else if ( global : : deviceID = = " n613 \n " ) {
2023-07-11 06:22:48 -07:00
setBrightness_ntxio ( brightnessValue ) ;
2021-06-28 12:26:04 -07:00
}
else {
2023-07-11 06:22:48 -07:00
setBrightness ( brightnessValue ) ;
2021-06-28 12:26:04 -07:00
}
}
void cinematicBrightness ( int value , int mode ) {
2022-04-15 19:28:25 -07:00
/* mode can be 0, 1, or 2, respectively:
2021-06-28 12:26:04 -07:00
* 0 : Bring UP brightness
* 1 : Bring DOWN brightness
2022-04-15 19:28:25 -07:00
* 2 : Auto ; smooth brightness transition between two brightness levels
2021-06-28 12:26:04 -07:00
*/
2022-05-22 12:38:02 -07:00
if ( global : : deviceID ! = " n705 \n " & & global : : deviceID ! = " n905 \n " & & global : : deviceID ! = " kt \n " ) {
2022-04-16 16:40:30 -07:00
QString function = __func__ ; log ( function + " : Setting brightness to " + QString : : number ( value ) , " functions " ) ;
}
2021-06-28 12:26:04 -07:00
if ( mode = = 0 ) {
int brightness = 0 ;
while ( brightness ! = value ) {
brightness = brightness + 1 ;
2023-07-11 06:22:48 -07:00
preSetBrightness ( brightness ) ;
2021-06-28 13:13:44 -07:00
QThread : : msleep ( 30 ) ;
2021-06-28 12:26:04 -07:00
}
}
2022-04-15 19:28:25 -07:00
else if ( mode = = 1 ) {
2023-07-11 06:22:48 -07:00
int brightness = getBrightness ( ) ;
2021-06-28 12:26:04 -07:00
while ( brightness ! = 0 ) {
brightness = brightness - 1 ;
2023-07-11 06:22:48 -07:00
preSetBrightness ( brightness ) ;
2021-06-28 13:13:44 -07:00
QThread : : msleep ( 30 ) ;
2021-06-28 12:26:04 -07:00
}
}
2022-04-15 19:28:25 -07:00
else if ( mode = = 2 ) {
2023-07-11 06:22:48 -07:00
int brightness = getBrightness ( ) ;
2022-04-15 19:28:25 -07:00
if ( brightness < = value ) {
while ( brightness ! = value ) {
brightness = brightness + 1 ;
2023-07-11 06:22:48 -07:00
preSetBrightness ( brightness ) ;
2022-04-15 19:28:25 -07:00
QThread : : msleep ( 30 ) ;
}
}
else if ( brightness > = value ) {
while ( brightness ! = value ) {
brightness = brightness - 1 ;
2023-07-11 06:22:48 -07:00
preSetBrightness ( brightness ) ;
2022-04-15 19:28:25 -07:00
QThread : : msleep ( 30 ) ;
}
}
}
2021-06-28 12:26:04 -07:00
}
2023-07-11 06:22:48 -07:00
int getWarmth ( ) {
2021-07-16 13:41:36 -07:00
QString sysfsWarmthPath ;
2023-07-20 12:11:12 -07:00
int warmthValue ;
2022-04-02 13:49:18 -07:00
if ( global : : deviceID = = " n873 \n " ) {
2021-07-16 13:41:36 -07:00
sysfsWarmthPath = " /sys/class/backlight/lm3630a_led/color " ;
}
2023-07-20 12:11:12 -07:00
else if ( global : : deviceID = = " n249 \n " ) {
sysfsWarmthPath = " /sys/class/backlight/backlight_warm/actual_brightness " ;
}
2023-07-11 06:22:48 -07:00
QString warmthConfig = readFile ( sysfsWarmthPath ) ;
2023-07-20 12:11:12 -07:00
warmthValue = warmthConfig . toInt ( ) ;
if ( global : : deviceID = = " n873 \n " ) {
warmthValue = 10 - warmthValue ;
}
2021-07-16 13:41:36 -07:00
return warmthValue ;
}
2023-07-11 06:22:48 -07:00
void setWarmth ( int warmthValue ) {
QString sysfsWarmthPath ;
2023-07-20 12:11:12 -07:00
QString warmthValueStr ;
2022-04-02 13:49:18 -07:00
if ( global : : deviceID = = " n873 \n " ) {
2023-07-20 12:11:12 -07:00
// Value 0 gives a warmer lighting than value 10
warmthValue = 10 - warmthValue ;
warmthValueStr = QString : : number ( warmthValue ) ;
2021-07-16 13:41:36 -07:00
sysfsWarmthPath = " /sys/class/backlight/lm3630a_led/color " ;
}
2023-07-20 12:11:12 -07:00
else if ( global : : deviceID = = " n249 \n " ) {
warmthValueStr = QString : : number ( warmthValue ) ;
sysfsWarmthPath = " /sys/class/backlight/backlight_warm/brightness " ;
}
2023-07-11 06:22:48 -07:00
writeFile ( sysfsWarmthPath , warmthValueStr ) ;
2021-07-16 13:41:36 -07:00
}
2023-06-19 17:55:26 -07:00
void cinematicWarmth ( int warmthValue ) {
2023-07-11 06:22:48 -07:00
int currentWarmth = getWarmth ( ) ;
2023-06-19 17:55:26 -07:00
if ( warmthValue < currentWarmth ) {
while ( warmthValue < currentWarmth ) {
currentWarmth - - ;
2023-07-11 06:22:48 -07:00
setWarmth ( currentWarmth ) ;
2023-06-19 17:55:26 -07:00
QThread : : msleep ( 30 ) ;
}
}
else if ( warmthValue > currentWarmth ) {
while ( warmthValue > currentWarmth ) {
currentWarmth + + ;
2023-07-11 06:22:48 -07:00
setWarmth ( currentWarmth ) ;
2023-06-19 17:55:26 -07:00
QThread : : msleep ( 30 ) ;
}
}
}
2021-08-19 05:23:18 -07:00
void installUpdate ( ) {
2022-04-15 17:27:58 -07:00
log ( " Installing update package " , " functions " ) ;
2022-09-27 04:05:09 -07:00
writeFile ( " /mnt/onboard/onboard/.inkbox/can_really_update " , " true \n " ) ;
writeFile ( " /external_root/opt/update/will_update " , " true \n " ) ;
2023-07-11 06:22:48 -07:00
writeFile ( " /external_root/boot/flags/WILL_UPDATE " , " true \n " ) ;
2021-08-19 05:23:18 -07:00
reboot ( true ) ;
}
2021-11-14 18:22:58 -08:00
bool getEncFSStatus ( ) {
2021-11-14 19:23:09 -08:00
return checkconfig ( " /external_root/run/encfs_mounted " ) ;
2021-11-14 18:22:58 -08:00
}
2022-02-12 20:54:46 -08:00
bool isUsbPluggedIn ( ) {
2022-05-23 12:24:21 -07:00
if ( global : : deviceID = = " kt \n " ) {
if ( readFile ( " /sys/devices/system/yoshi_battery/yoshi_battery0/battery_status " ) = = " 1 \n " ) {
return 1 ;
}
else {
return 0 ;
}
}
2023-07-20 12:46:17 -07:00
else if ( global : : deviceID = = " n249 \n " ) {
2023-07-20 19:10:48 -07:00
if ( readFile ( " /sys/class/power_supply/rn5t618-battery/status " ) ! = " Discharging \n " ) {
2023-07-20 12:46:17 -07:00
return 1 ;
}
else {
return 0 ;
}
}
2022-05-23 12:24:21 -07:00
else {
2022-05-22 12:38:02 -07:00
// Thanks to https://github.com/koreader/KoboUSBMS/blob/2efdf9d920c68752b2933f21c664dc1afb28fc2e/usbms.c#L148-L158
int ntxfd ;
if ( ( ntxfd = open ( " /dev/ntx_io " , O_RDWR ) ) = = - 1 ) {
fprintf ( stderr , " Error opening ntx_io device \n " ) ;
}
unsigned long ptr = 0U ;
ioctl ( ntxfd , 108 , & ptr ) ;
close ( ntxfd ) ;
return ! ! ptr ;
2022-02-12 20:54:46 -08:00
}
}
2022-06-18 06:26:52 -07:00
void updateUserAppsMainJsonFile ( ) {
QDirIterator appsDir ( " /mnt/onboard/onboard/.apps " , QDirIterator : : NoIteratorFlags ) ;
QFile newJsonFile = QFile { " /mnt/onboard/onboard/.apps/apps.json " } ;
QJsonDocument newJsonDocument ;
QJsonArray array ;
while ( appsDir . hasNext ( ) )
{
QDir dir ( appsDir . next ( ) ) ;
if ( dir . exists ( ) = = true ) {
if ( dir . path ( ) . split ( " / " ) . last ( ) . contains ( " . " ) = = false ) {
QFile jsonSmall = QFile { dir . path ( ) + " /app.json " } ;
if ( jsonSmall . exists ( ) = = true ) {
jsonSmall . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
QString jsonString = jsonSmall . readAll ( ) ;
jsonSmall . close ( ) ;
QJsonDocument jsonSmallDoc = QJsonDocument : : fromJson ( jsonString . toUtf8 ( ) ) ;
if ( jsonSmallDoc [ " app " ] . isObject ( ) = = true ) {
QJsonObject jsonSmallMainObj = jsonSmallDoc [ " app " ] . toObject ( ) ;
array . append ( jsonSmallMainObj ) ;
}
else {
log ( " Error: User application ' " + appsDir . path ( ) + " ''s JSON file descriptor is missing main object 'app' " , " main " ) ;
}
}
else {
QString message = " User application ' " + appsDir . path ( ) + " ' does not contain any 'app.json' file: " ;
message . append ( jsonSmall . fileName ( ) ) ;
log ( message , " main " ) ;
}
}
}
}
// https://forum.qt.io/topic/104791/how-i-can-create-json-format-in-qt/5
QJsonObject root ;
root [ " list " ] = array ;
newJsonDocument . setObject ( root ) ;
newJsonFile . open ( QFile : : WriteOnly | QFile : : Text | QFile : : Truncate ) ;
newJsonFile . write ( newJsonDocument . toJson ( ) ) ;
newJsonFile . flush ( ) ;
newJsonFile . close ( ) ;
}
void updateUserAppsSmallJsonFiles ( ) {
QFile jsonFile = QFile { " /mnt/onboard/onboard/.apps/apps.json " } ;
jsonFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
QString fileRead = jsonFile . readAll ( ) ;
jsonFile . close ( ) ;
QJsonDocument jsonDocument = QJsonDocument : : fromJson ( fileRead . toUtf8 ( ) ) ;
if ( jsonDocument [ " list " ] . isArray ( ) = = true ) {
QJsonArray jsonArray = jsonDocument [ " list " ] . toArray ( ) ;
for ( QJsonValueRef refJsonObject : jsonArray ) {
QJsonObject jsonMainObject = refJsonObject . toObject ( ) ;
QString appName = jsonMainObject [ " Name " ] . toString ( ) ;
// This needs to be here and not at the beggining of this function because it is an iterator
QDirIterator appsDir ( " /mnt/onboard/onboard/.apps " , QDirIterator : : NoIteratorFlags ) ;
while ( appsDir . hasNext ( ) ) {
QDir dir ( appsDir . next ( ) ) ;
if ( dir . exists ( ) = = true ) {
if ( dir . path ( ) . split ( " / " ) . last ( ) . toLower ( ) . contains ( appName . toLower ( ) ) = = true ) {
QJsonObject root ;
root [ " app " ] = refJsonObject . toObject ( ) ; ;
QJsonDocument newJsonDocument ;
newJsonDocument . setObject ( root ) ;
QFile newSmallJson = QFile { dir . path ( ) + " / " + " app.json " } ;
newSmallJson . open ( QIODevice : : ReadWrite ) ;
QTextStream stream ( & newSmallJson ) ;
stream < < newJsonDocument . toJson ( ) < < Qt : : endl ;
newSmallJson . flush ( ) ;
newSmallJson . close ( ) ;
}
}
}
}
}
}
2022-08-08 20:38:38 -07:00
QString fileChecksum ( const QString & fileName , QCryptographicHash : : Algorithm hashAlgorithm ) {
2022-06-28 23:15:13 -07:00
QFile f ( fileName ) ;
if ( f . open ( QFile : : ReadOnly ) ) {
QCryptographicHash hash ( hashAlgorithm ) ;
if ( hash . addData ( & f ) ) {
2022-08-08 20:38:38 -07:00
return hash . result ( ) . toHex ( ) ;
2022-06-28 23:15:13 -07:00
}
}
}
2022-07-06 11:24:23 -07:00
QJsonObject getBookMetadata ( int bookID ) {
// Read library database from file
QFile database ( global : : localLibrary : : databasePath ) ;
QByteArray data ;
if ( database . open ( QIODevice : : ReadOnly ) ) {
data = database . readAll ( ) ;
database . close ( ) ;
}
else {
QString function = __func__ ; log ( function + " : Failed to open local library database file for reading at ' " + database . fileName ( ) + " ' " , " functions " ) ;
}
// Parse JSON data
QJsonObject jsonObject = QJsonDocument : : fromJson ( qUncompress ( QByteArray : : fromBase64 ( data ) ) ) . object ( ) ;
QJsonArray jsonArrayList = jsonObject [ " database " ] . toArray ( ) ;
return jsonArrayList . at ( bookID - 1 ) . toObject ( ) ;
}
2022-08-08 16:57:45 -07:00
QJsonObject readHighlightsDatabase ( ) {
// Read highlights database from file
QFile database ( global : : localLibrary : : highlightsDatabasePath ) ;
QByteArray data ;
if ( database . open ( QIODevice : : ReadOnly ) ) {
data = database . readAll ( ) ;
database . close ( ) ;
}
else {
QString function = __func__ ; log ( function + " : Failed to open highlights database file for reading at ' " + database . fileName ( ) + " ' " , " functions " ) ;
}
// Parse JSON data
return QJsonDocument : : fromJson ( qUncompress ( QByteArray : : fromBase64 ( data ) ) ) . object ( ) ;
}
void writeHighlightsDatabase ( QJsonObject jsonObject ) {
QFile : : remove ( global : : localLibrary : : highlightsDatabasePath ) ;
writeFile ( global : : localLibrary : : highlightsDatabasePath , qCompress ( QJsonDocument ( jsonObject ) . toJson ( ) ) . toBase64 ( ) ) ;
}
2022-09-13 05:20:37 -07:00
QJsonDocument readTodoDatabase ( ) {
// Read To-Do database from file
QFile database ( global : : localLibrary : : todoDatabasePath ) ;
QByteArray data ;
if ( database . open ( QIODevice : : ReadOnly ) ) {
data = database . readAll ( ) ;
database . close ( ) ;
}
else {
QString function = __func__ ; log ( function + " : Failed to open To-Do database file for reading at ' " + database . fileName ( ) + " ' " , " functions " ) ;
}
// Parse JSON data
return QJsonDocument : : fromJson ( data ) ;
}
void writeTodoDatabase ( QJsonDocument jsonDocument ) {
QFile : : remove ( global : : localLibrary : : todoDatabasePath ) ;
writeFile ( global : : localLibrary : : todoDatabasePath , jsonDocument . toJson ( ) ) ;
}
2022-08-08 16:57:45 -07:00
void highlightBookText ( QString text , QString bookPath , bool remove ) {
if ( remove = = false ) {
if ( ! QFile : : exists ( global : : localLibrary : : highlightsDatabasePath ) ) {
QJsonObject mainJsonObject ;
QJsonObject firstJsonObject ;
firstJsonObject . insert ( " BookPath " , QJsonValue ( bookPath ) ) ;
2022-08-09 12:41:50 -07:00
firstJsonObject . insert ( " T- " + QUuid : : createUuid ( ) . toString ( QUuid : : WithoutBraces ) , QJsonValue ( text ) ) ;
2022-08-08 16:57:45 -07:00
mainJsonObject [ " Book1 " ] = firstJsonObject ;
writeHighlightsDatabase ( mainJsonObject ) ;
}
else {
QJsonObject jsonObject = readHighlightsDatabase ( ) ;
bool highlightWrote = false ;
int length = jsonObject . length ( ) ;
for ( int i = 1 ; i < = length ; i + + ) {
if ( jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) . value ( " BookPath " ) . toString ( ) = = bookPath ) {
2022-08-09 12:41:50 -07:00
log ( " highlightBookText: Found existing book in database with path ' " + bookPath + " ' " , " functions " ) ;
2022-08-08 16:57:45 -07:00
// Insert highlight
QJsonObject highlightJsonObject = jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) ;
// Finding available slot for highlight in case the one we are looking for is already occupied
2022-08-09 12:41:50 -07:00
QString uuid = " T- " + QUuid : : createUuid ( ) . toString ( QUuid : : WithoutBraces ) ;
if ( highlightJsonObject . contains ( uuid ) ) {
2022-08-08 16:57:45 -07:00
while ( true ) {
2022-08-09 12:41:50 -07:00
if ( highlightJsonObject . contains ( uuid ) ) {
uuid = " T- " + QUuid : : createUuid ( ) . toString ( QUuid : : WithoutBraces ) ;
2022-08-08 16:57:45 -07:00
}
else {
break ;
}
}
}
2022-08-09 12:41:50 -07:00
highlightJsonObject . insert ( uuid , text ) ;
2022-08-08 16:57:45 -07:00
jsonObject [ " Book " + QString : : number ( i ) ] = highlightJsonObject ;
writeHighlightsDatabase ( jsonObject ) ;
highlightWrote = true ;
}
}
if ( highlightWrote = = false ) {
2022-08-09 12:41:50 -07:00
// This block of code is called when the book is referenced in the database, but no highlights are currently indexed
2022-08-08 16:57:45 -07:00
QJsonObject bookJsonObject ;
bookJsonObject . insert ( " BookPath " , QJsonValue ( bookPath ) ) ;
2022-08-09 12:41:50 -07:00
bookJsonObject . insert ( " T- " + QUuid : : createUuid ( ) . toString ( QUuid : : WithoutBraces ) , QJsonValue ( text ) ) ;
2022-08-08 16:57:45 -07:00
jsonObject [ " Book " + QString : : number ( length + 1 ) ] = bookJsonObject ;
writeHighlightsDatabase ( jsonObject ) ;
highlightWrote = true ;
}
}
}
else {
QJsonObject jsonObject = readHighlightsDatabase ( ) ;
int length = jsonObject . length ( ) ;
for ( int i = 1 ; i < = length ; i + + ) {
if ( jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) . value ( " BookPath " ) . toString ( ) = = bookPath ) {
QJsonObject bookJsonObject = jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) ;
foreach ( const QString & key , bookJsonObject . keys ( ) ) {
if ( bookJsonObject . value ( key ) . toString ( ) = = text ) {
log ( " Found matching highlight to remove with text ' " + text + " ' " , " functions.h " ) ;
bookJsonObject . remove ( key ) ;
}
}
jsonObject [ " Book " + QString : : number ( i ) ] = bookJsonObject ;
writeHighlightsDatabase ( jsonObject ) ;
}
}
}
}
QJsonObject getHighlightsForBook ( QString bookPath ) {
QJsonObject jsonObject = readHighlightsDatabase ( ) ;
int length = jsonObject . length ( ) ;
for ( int i = 1 ; i < = length ; i + + ) {
if ( jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) . value ( " BookPath " ) . toString ( ) = = bookPath ) {
return jsonObject [ " Book " + QString : : number ( i ) ] . toObject ( ) ;
break ;
}
}
return QJsonObject ( ) ;
}
2022-07-12 09:34:36 -07:00
float determineYIncrease ( ) {
if ( global : : deviceID = = " n705 \n " or global : : deviceID = = " n905 \n " ) {
return 2 ;
}
2022-08-19 13:01:06 -07:00
else if ( global : : deviceID = = " n613 \n " or global : : deviceID = = " n236 \n " or global : : deviceID = = " n306 \n " or global : : deviceID = = " emu \n " ) {
2022-07-12 09:34:36 -07:00
return 2.6 ;
}
2023-07-20 12:11:12 -07:00
else if ( global : : deviceID = = " n437 \n " or global : : deviceID = = " n249 \n " or global : : deviceID = = " n873 \n " ) {
2022-07-12 09:34:36 -07:00
return 3 ;
}
else {
return 2 ;
}
}
2022-08-22 06:42:39 -07:00
global : : wifi : : wifiState checkWifiState ( ) {
2023-06-09 07:17:37 -07:00
QString interfaceName ;
2023-07-20 12:11:12 -07:00
if ( global : : deviceID = = " n437 \n " or global : : deviceID = = " n249 \n " or global : : deviceID = = " kt \n " ) {
2023-06-09 07:17:37 -07:00
interfaceName = " wlan0 " ;
}
else {
interfaceName = " eth0 " ;
}
2023-06-10 07:03:12 -07:00
// Check if network interface has an IP address
QNetworkInterface iface = QNetworkInterface : : interfaceFromName ( interfaceName ) ;
QList < QNetworkAddressEntry > entries = iface . addressEntries ( ) ;
if ( ! entries . isEmpty ( ) ) {
// Interface is up and has an IP address
2023-06-09 07:17:37 -07:00
global : : wifi : : isConnected = true ;
return global : : wifi : : wifiState : : configured ;
}
else {
2023-06-10 07:03:12 -07:00
if ( QFile : : exists ( " /sys/class/net/ " + interfaceName + " /operstate " ) ) {
// Interface is up but doesn't have an IP address
global : : wifi : : isConnected = false ;
return global : : wifi : : wifiState : : enabled ;
}
else {
// Interface is not up
global : : wifi : : isConnected = false ;
return global : : wifi : : wifiState : : disabled ;
}
2022-08-15 12:10:32 -07:00
}
}
2024-01-13 18:18:28 -08:00
int testPing ( QString ipAddress = " 1.1.1.1 " ) {
2022-08-15 12:10:32 -07:00
// For some reason, implementing a non-blocking version of this functions triggers a "terminate called without an active exception" error with a platform plugin compiled with a newer GCC 11 toolchain. The problem has been solved by transplanting this function into the related area which uses it.
2024-01-13 18:18:28 -08:00
QString function = __func__ ; log ( function + " : pinging IP address " + ipAddress , " functions " ) ;
2022-08-15 12:10:32 -07:00
QProcess * pingProcess = new QProcess ( ) ;
QString pingProg = " ping " ;
QStringList pingArgs ;
2024-01-13 18:18:28 -08:00
pingArgs < < " -c " < < " 1 " < < ipAddress ;
2022-08-15 12:10:32 -07:00
pingProcess - > start ( pingProg , pingArgs ) ;
pingProcess - > waitForFinished ( ) ;
int exitCode = pingProcess - > exitCode ( ) ;
pingProcess - > deleteLater ( ) ;
if ( exitCode = = 0 ) {
2024-01-13 18:18:28 -08:00
log ( " Ping successful " , " functions " ) ;
2022-08-15 12:10:32 -07:00
global : : wifi : : isConnected = true ;
}
else {
2024-01-13 18:18:28 -08:00
log ( " Ping unsuccessful " , " functions " ) ;
if ( ipAddress = = " 1.1.1.1 " ) {
global : : wifi : : isConnected = false ;
}
2022-08-15 12:10:32 -07:00
}
return exitCode ;
}
2022-08-18 12:47:08 -07:00
bool checkProcessName ( QString name ) {
QDirIterator appsDir ( " /proc " , QDirIterator : : NoIteratorFlags ) ;
while ( appsDir . hasNext ( ) ) {
QDir dir ( appsDir . next ( ) ) ;
QFile process = QFile ( dir . path ( ) + " /cmdline " ) ;
if ( process . exists ( ) = = true ) {
process . open ( QIODevice : : ReadOnly ) ;
QTextStream stream ( & process ) ;
if ( stream . readLine ( ) . contains ( name ) = = true ) {
process . close ( ) ;
return true ;
}
process . close ( ) ;
}
}
return false ;
}
2022-10-15 07:35:03 -07:00
QString purgeHtml ( QString text ) {
// https://stackoverflow.com/questions/2799379/is-there-an-easy-way-to-strip-html-from-a-qstring-in-qt
2022-10-16 19:22:46 -07:00
// This can cause problems if someone names their directory with HTML tags, so stop here. Anki, which is a big project, also doesn't care about this
2022-10-15 07:35:03 -07:00
return text . remove ( QRegExp ( " <[^>]*> " ) ) ;
}
2023-07-24 12:15:56 -07:00
void bool_writeconfig ( QString file , bool option ) {
QString str ;
if ( option = = true ) {
str = " true " ;
}
else {
str = " false " ;
}
std : : ofstream fhandler ;
fhandler . open ( file . toStdString ( ) ) ;
fhandler < < str . toStdString ( ) ;
fhandler . close ( ) ;
}
2021-04-05 06:50:58 -07:00
}
2021-07-16 13:41:36 -07:00
2021-04-05 06:50:58 -07:00
# endif // FUNCTIONS_H