-
Notifications
You must be signed in to change notification settings - Fork 0
/
appnotex.cpp
170 lines (143 loc) · 6.09 KB
/
appnotex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <pwd.h>
#include <sys/types.h>
#include <filesystem>
#include <fstream>
#include <string>
#include "alib.hpp"
#include "database.hpp"
#include "errorcodes.hpp"
#include "export/export.hpp"
#include "json/json.h"
#include "version.hpp"
int main(int argc, char const *argv[]) {
// Getting the home directory
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
std::string home = homedir;
// Checking if the config dir & file exists or not
std::string config_dir = home + "/.config/appnotex";
if (!std::filesystem::exists(config_dir)) {
std::cerr << "Config directory not found. Creating the directory for first "
"time....."
<< std::endl;
mkdir(config_dir.c_str(), 0777);
}
std::string config_file = home + "/.config/appnotex/config.json";
if (!std::filesystem::exists(config_file)) {
std::cerr << rang::fg::red << "Error: " << rang::fg::reset
<< "Missing configuration file. Please create or copy the "
"example config file from /usr/share/doc/appnotex/config.json "
"to the $HOME/.config/appnotex folder & "
"then try again"
<< std::endl;
return CONFIGNOTFOUND;
}
// loading the configuration file
std::ifstream config(config_file);
Json::Value root;
config >> root;
// ------------------------------------------------AppNotEx------------------------------------
// Displaying the title of the AppNotEx using Alib decorators depending on
// the user config. The default is to clear the screen first
bool doClear = root.get("startupScreenClear", true).asBool();
if (doClear) alib::clrscr();
alib::decorateMe("AppNotEx", 1, "", true);
// Creating database & Table
std::string dbfile =
home + root.get("dbsavedir", "/.local/share/data.db").asString();
const char *dbfilename = dbfile.c_str();
const char *tbname = "Data";
Database *db = new Database();
if (argc >= 2) {
// If cmd line arguments are passed then do stuffs here
const char *availableCmdArgs[] = {
"-e", "--export", "-p", "--print", "-h", "--help",
"-g", "--general", "--printall", "-v", "--version"};
if (std::strcmp(argv[1], availableCmdArgs[0]) == 0 ||
strcmp(argv[1], availableCmdArgs[1]) == 0) { /* --export || -e */
Export *appnotexExport = new Export();
int status = appnotexExport->pipeSave("appnotex-exported-data.txt");
if (status == CMDEXECFAILED) {
std::cerr << rang::fg::red << rang::style::bold
<< "Error: Could not export and create data file"
<< rang::fg::reset << rang::style::reset << std::endl;
} else {
std::cout << "The exported data have been saved as "
<< rang::style::bold << "appnotex-exported-data.txt "
<< rang::style::reset << "in the current directory"
<< std::endl;
}
} else if (std::strcmp(argv[1], availableCmdArgs[2]) == 0 ||
std::strcmp(argv[1], availableCmdArgs[3]) ==
0) { /* --print || -p */
db->printData(dbfilename, tbname);
} else if (std::strcmp(argv[1], availableCmdArgs[6]) == 0 ||
std::strcmp(argv[1], availableCmdArgs[7]) ==
0) { /* -g || --general note taking || notex */
system("notex");
} else if (std::strcmp(argv[1], availableCmdArgs[8]) ==
0) { /* --printall */
db->printData(dbfilename, tbname);
alib::decorateMe("Notex", 1, "", true);
db->printNotexData(dbfilename, "general");
alib::horizontalLine(1, "blue");
} else if (std::strcmp(argv[1], availableCmdArgs[9]) == 0 ||
std::strcmp(argv[1], availableCmdArgs[10]) ==
0) { /* --version || -v */
bool isPrettyVersionEnabled = root.get("prettyVersion", true).asBool();
if (isPrettyVersionEnabled) alib ::horizontalLine();
std::cout << std::endl;
std::cout << "Current appnotex version: " << appnotex::info::appnotexVer
<< std::endl;
std::cout << "Developed By: " << developer << std::endl;
std::cout << "License: " << license << std::endl;
if (isPrettyVersionEnabled) alib::horizontalLine();
}
else { /* Handle invalid or unsupported arguements and the help argument
*/
// Show invalid arg notice if --help || -h is not given
if (std::strcmp(argv[1], availableCmdArgs[4]) == 0 ||
std::strcmp(argv[1], availableCmdArgs[5]) == 0) {
std::cout << rang::bg::black << rang::style::underline
<< rang::style::bold << " AppNotEx Help Menu \n\n"
<< rang::bg::reset << rang::style::reset;
} else {
std::cout << "Invalid arguments passed. available commands are: ";
std::cout << std::endl;
std::cout << std::endl;
}
// Printing the available commands array
std::cout << rang::style::italic << rang::fg::green
<< "AppNotEx commands: " << rang::style::reset
<< rang::fg::reset;
for (int i = 0; i < sizeof availableCmdArgs / sizeof availableCmdArgs[0];
i++) {
std::cout << availableCmdArgs[i] << " ";
}
}
} else {
// if no cmd line arguments are passed then do stuffs here
db->createDb(dbfilename);
db->createTable(dbfilename, tbname);
std::string appnameData, distroname, link, note;
std::cout << "App Name: ";
getline(std::cin, appnameData);
std::cout << "Distro Name: ";
getline(std::cin, distroname);
std::cout << "App Link: ";
getline(std::cin, link);
std::cout << "Extra Notes: ";
getline(std::cin, note);
bool queryStatus =
db->insertData(dbfilename, tbname, appnameData.c_str(),
distroname.c_str(), link.c_str(), note.c_str());
if (queryStatus)
alib::decorateMe("Records kept successfully", 0, "*");
else
std::cerr << rang::fg::red << "Errors occured ! Couldn't store data"
<< rang::fg::reset << std::endl;
}
// Free up the memory by deleting the Databse object
delete db;
return 0;
}