Application.cpp (5041B)
1 #include "Application.hpp" 2 #include <cstdlib> 3 #include <fstream> 4 #include <iostream> 5 6 Application::Application() 7 :sesErreurs("") 8 ,sonCopier("") 9 ,sonRepertoireCourant("") 10 { 11 std::ifstream laConfiguration("config.txt"); 12 std::string laVariable, laValeur; 13 14 while (laConfiguration.good() == true) 15 { 16 getline(laConfiguration, laVariable, '='); 17 getline(laConfiguration, laValeur, '\n'); 18 19 if (laVariable == "" || laValeur == "" || saConfiguration.insert(std::make_pair(laVariable, laValeur)).second == false) 20 { 21 std::cerr << "Application(): " << "The config.txt file is corrupted" << std::endl; 22 exit(EXIT_FAILURE); 23 } 24 25 } 26 27 laConfiguration.close(); 28 sonRepertoireCourant = getSaValeur("default-directory"); 29 std::ifstream lesApplications("apps.txt"); 30 std::string lExtension, lApplication; 31 32 while (lesApplications.good() == true) 33 { 34 getline(lesApplications, lExtension, '\t'); 35 getline(lesApplications, lApplication, '\n'); 36 37 if (lExtension == "" || lApplication == "" || sesApplications.insert(std::make_pair(lExtension, lApplication)).second == false) 38 { 39 ajouterErreur("The apps.txt file is corrupted."); 40 } 41 42 } 43 44 lesApplications.close(); 45 std::vector<std::vector<Element *> >::iterator leItY; 46 std::vector<Element *>::iterator leItX; 47 sonContenu.resize(1000); 48 49 for (leItY = sonContenu.begin(); leItY != sonContenu.end(); leItY++) 50 { 51 leItY->resize(atoi(getSaValeur("nb-icons-x").c_str())); 52 53 for (leItX = leItY->begin(); leItX != leItY->end(); leItX++) 54 { 55 *leItX = NULL; 56 } 57 58 } 59 60 } 61 62 Application::~Application() 63 { 64 std::vector<std::vector<Element *> >::iterator leItY; 65 std::vector<Element *>::iterator leItX; 66 67 for (leItY = sonContenu.begin(); leItY != sonContenu.end(); leItY++) 68 { 69 70 for (leItX = leItY->begin(); leItX != leItY->end(); leItX++) 71 { 72 delete *leItX; 73 } 74 75 } 76 77 } 78 79 void Application::ajouterErreur(std::string uneErreur) 80 { 81 sesErreurs += uneErreur + '\n'; 82 return; 83 } 84 85 void Application::changerRepertoire(std::string unRepertoire) 86 { 87 88 if (unRepertoire == "..") 89 { 90 91 if (sonRepertoireCourant != "/") 92 { 93 int i = sonRepertoireCourant.find_last_of('/', sonRepertoireCourant.size() - 2); 94 sonRepertoireCourant.erase(i + 1); 95 } 96 97 } 98 99 else 100 { 101 sonRepertoireCourant += unRepertoire + '/'; 102 } 103 104 return; 105 } 106 107 void Application::coller(std::string unRepertoire) 108 { 109 110 if (sonCopier != "") 111 { 112 std::string laCommande = "cp -Rf \"" + sonCopier + "\" \"" + sonRepertoireCourant + unRepertoire + "\";"; 113 114 if (system(laCommande.c_str()) == -1) 115 { 116 ajouterErreur("Permission denied."); 117 } 118 119 } 120 121 return; 122 } 123 124 void Application::copier(std::string unFichier) 125 { 126 sonCopier = sonRepertoireCourant + unFichier; 127 return; 128 } 129 130 std::string Application::getSaValeur(std::string uneVariable) const 131 { 132 std::map<std::string, std::string>::const_iterator leIt = saConfiguration.find(uneVariable); 133 134 if (leIt == saConfiguration.end()) 135 { 136 std::cerr << "getSaValeur(): " << "The config.txt file is corrupted" << std::endl; 137 exit(EXIT_FAILURE); 138 } 139 140 return leIt->second; 141 } 142 143 std::string Application::getSesErreurs() const 144 { 145 return sesErreurs; 146 } 147 148 std::string Application::getSonApplication(std::string uneExtension) const 149 { 150 std::string uneApplication = ""; 151 std::map<std::string, std::string>::const_iterator leIt = sesApplications.find(uneExtension); 152 153 if (leIt != sesApplications.end()) 154 { 155 uneApplication = leIt->second; 156 } 157 158 return uneApplication; 159 } 160 161 std::vector<std::vector<Element *> > Application::getSonContenu() const 162 { 163 return sonContenu; 164 } 165 166 std::string Application::getSonRepertoireCourant() const 167 { 168 return sonRepertoireCourant; 169 } 170 171 void Application::lireRepertoire() 172 { 173 std::vector<std::vector<Element *> >::iterator leItY; 174 std::vector<Element *>::iterator leItX; 175 176 for (leItY = sonContenu.begin(); leItY != sonContenu.end(); leItY++) 177 { 178 179 for (leItX = leItY->begin(); leItX != leItY->end(); leItX++) 180 { 181 delete *leItX; 182 *leItX = NULL; 183 } 184 185 } 186 187 DIR * leRepertoire = opendir(sonRepertoireCourant.c_str()); 188 189 if (leRepertoire == NULL) 190 { 191 ajouterErreur("Permission denied."); 192 changerRepertoire(".."); 193 lireRepertoire(); 194 } 195 196 else 197 { 198 dirent * lEntree = NULL; 199 int leX = 0; 200 int leXMax = atoi(getSaValeur("nb-icons-x").c_str()); 201 int leY = 0; 202 bool estVide = true; 203 204 while ((lEntree = readdir(leRepertoire)) != NULL) 205 { 206 207 if (leX == leXMax) 208 { 209 leX = 0; 210 leY++; 211 } 212 213 if (sizeof(lEntree->d_name) != 0 && lEntree->d_name[0] != '.') 214 { 215 estVide = false; 216 sonContenu[leY][leX] = new Element(lEntree); 217 leX++; 218 } 219 220 } 221 222 closedir(leRepertoire); 223 224 if (estVide == true) 225 { 226 ajouterErreur("There are no files."); 227 changerRepertoire(".."); 228 lireRepertoire(); 229 } 230 231 } 232 233 return; 234 } 235 236 void Application::redemarrer() const 237 { 238 execlp("reboot", "reboot", NULL); 239 return; 240 } 241 242 void Application::resetSesErreurs() 243 { 244 sesErreurs.clear(); 245 return; 246 } 247 248 void Application::supprimer(std::string unFichier) 249 { 250 std::string laCommande = "rm -Rf \"" + sonRepertoireCourant + unFichier + "\";"; 251 252 if (system(laCommande.c_str()) == -1) 253 { 254 ajouterErreur("Permission denied."); 255 } 256 257 return; 258 } 259