Advertisement
Guest User

pbsystemtab.cpp

a guest
Dec 27th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1. /****************************************************************************
  2. ** ui.h extension file, included from the uic-generated form implementation.
  3. **
  4. ** If you want to add, delete, or rename functions or slots, use
  5. ** Qt Designer to update this file, preserving your code.
  6. **
  7. ** You should not define a constructor or destructor in this file.
  8. ** Instead, write your code in functions called init() and destroy().
  9. ** These will automatically be called by the form's constructor and
  10. ** destructor.
  11. *****************************************************************************/
  12. #include <qstring.h>
  13. #include <pcbsd-utils.h>
  14. #include <qfiledialog.h>
  15. #include <QTextStream>
  16. #include <QNetworkAccessManager>
  17. #include <QNetworkReply>
  18. #include <QNetworkProxy>
  19. #include <QProgressDialog>
  20. #include <QDebug>
  21. #include "../config.h"
  22.  
  23. #include "pbsystemtab.h"
  24.  
  25. void PBSystemTab::ProgramInit()
  26. {
  27. //Grab the username
  28. username = QString::fromLocal8Bit(getenv("LOGNAME"));
  29.  
  30. // Set the Uname on the General Tab
  31. CheckUname();
  32. // Set the PC-BSD Version on the General Tab
  33. CheckPBVer();
  34.  
  35. // See if we need to setup a proxy
  36. checkProxy();
  37.  
  38. //Get & Set CPU Type
  39. labelCPU->setText(pcbsd::Utils::sysctl("hw.model").simplified());
  40.  
  41. //Get & Set RAM
  42. labelMemory->setText(pcbsd::Utils::bytesToHumanReadable(pcbsd::Utils::sysctlAsInt("hw.physmem")));
  43.  
  44. // Read any kernel settings
  45. //LoadKernSettings();
  46.  
  47. // Read any rc.conf settings
  48. //loadServSettings();
  49.  
  50. //Load boot screen data
  51. loadBootData();
  52.  
  53. // Load the default package set value
  54. if ( pcbsd::Utils::getValFromPCBSDConf("PACKAGE_SET") == "EDGE" ) {
  55. radioEdge->setChecked(true);
  56. radioProduction->setChecked(false);
  57. } else {
  58. radioProduction->setChecked(true);
  59. radioEdge->setChecked(false);
  60. }
  61.  
  62. // Connect our various buttons
  63. connect(buttonGenerate, SIGNAL(clicked()), this, SLOT(startGenerateSheet()) );
  64. //connect(showBootCheck, SIGNAL(clicked()), this, SIGNAL(changed()));
  65.  
  66. connect(pushMiscSave, SIGNAL( clicked() ), this, SLOT( slotMiscSave() ) );
  67.  
  68. connect(fetchSourceBut, SIGNAL( clicked() ), this, SLOT( fetchSourcePressed() ) );
  69. connect(fetchPortsBut, SIGNAL( clicked() ), this, SLOT( fetchPortsPressed() ) );
  70. }
  71.  
  72. void PBSystemTab::CheckUname()
  73. {
  74. QString prog = "uname";
  75. QStringList args;
  76. args << "-rm";
  77.  
  78. GetUname = new QProcess( this );
  79. connect( GetUname, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadUname()) );
  80. GetUname->start(prog, args);
  81. }
  82.  
  83.  
  84. void PBSystemTab::CheckPBVer()
  85. {
  86.  
  87. QString pcVer="";
  88. QStringList out = pcbsd::Utils::runShellCommand(QString("pkg info -f pcbsd-base"));
  89. if (out.size()) {
  90. for (int i=0; i<out.size(); i++)
  91. {
  92. if (out[i].contains("Version"))
  93. {
  94. pcVer = out[i];
  95. pcVer.replace("Version : ","");
  96. labelPCBSD->setText(pcVer);
  97. break;
  98. }
  99. }
  100. }
  101.  
  102.  
  103. Arch = getLineFromCommandOutput("uname -m");
  104.  
  105. }
  106.  
  107.  
  108. void PBSystemTab::ReadUname()
  109. {
  110. if (GetUname->canReadLine() )
  111. {
  112. QString uname = GetUname->readLine().simplified();
  113. labelBASEVer->setText( uname );
  114. Version= uname.section(" ",0,0);
  115. }
  116. }
  117.  
  118.  
  119. // Ask user where to save text file, then start the generation
  120. void PBSystemTab::startGenerateSheet()
  121. {
  122. SheetFileName="";
  123. SheetFileName = QFileDialog::getSaveFileName(
  124. this,
  125. "save file dialog",
  126. "/home/" + username + "/Desktop",
  127. "Text File (*.txt)");
  128.  
  129. if ( ! SheetFileName.isEmpty() )
  130. {
  131. if ( SheetFileName.indexOf(".txt", 0) == -1)
  132. {
  133. SheetFileName.append(".txt");
  134. }
  135. if ( QFile::exists(SheetFileName ) )
  136. {
  137. int answer = QMessageBox::warning( this, "Overwrite File", "Overwrite " + SheetFileName + "?", "&Yes", "&No", QString::null, 1, 1 );
  138. if ( answer == 0 ) {
  139. CreateSheetFile();
  140. }
  141.  
  142. } else {
  143. CreateSheetFile();
  144. }
  145. }
  146.  
  147.  
  148. }
  149.  
  150.  
  151. void PBSystemTab::CreateSheetFile()
  152. {
  153.  
  154. SheetGenScript = new QProcess( this );
  155. QString prog = PREFIX + "/share/pcbsd/scripts/GenDiagSheet.sh";
  156. QStringList args;
  157. args << SheetFileName;
  158. connect( SheetGenScript, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finishedSheet()) );
  159.  
  160. SheetGenScript->start(prog, args);
  161. }
  162.  
  163.  
  164. void PBSystemTab::finishedSheet()
  165. {
  166. ViewSheet = new QProcess( this );
  167. QString prog = "kedit";
  168. QStringList args;
  169. args << SheetFileName;
  170. ViewSheet->start(prog, args);
  171. }
  172.  
  173.  
  174. void PBSystemTab::saveKernScreen()
  175. {
  176. if ( checkForceIbus->isChecked() )
  177. pcbsd::Utils::setConfFileValue(PREFIX + "/share/pcbsd/xstartup/enable-ibus.sh", "FORCEIBUS", "FORCEIBUS=\"YES\"", 1);
  178. else
  179. pcbsd::Utils::setConfFileValue(PREFIX + "/share/pcbsd/xstartup/enable-ibus.sh", "FORCEIBUS", "FORCEIBUS=\"NO\"", 1);
  180.  
  181. }
  182.  
  183.  
  184.  
  185. void PBSystemTab::showRebootRequired()
  186. {
  187. QMessageBox::information( this, "PC-BSD Notification",
  188. "Settings Updated!\n You must reboot for changes to take effect!" );
  189. }
  190.  
  191.  
  192. // Read the loader.conf, and load any settings we need
  193. /*void PBSystemTab::LoadKernSettings()
  194. {
  195. if ( checkValue("/boot/loader.conf", "splash_pcx_load=", "YES" ) ) { showBootCheck->setChecked(true); }
  196. else { showBootCheck->setChecked(false); }
  197. }
  198. */
  199. // Checks the file for a string KEY, and sees if its set to VALUE
  200. bool PBSystemTab::checkValue( QString File, QString Key, QString Value )
  201. {
  202. QString theValue = pcbsd::Utils::getConfFileValue(File, Key);
  203. if (theValue == Value) {
  204. return TRUE;
  205. }
  206. return FALSE;
  207.  
  208. }
  209.  
  210. void PBSystemTab::fetchSourcePressed()
  211. {
  212. portsnapUI = new PortsnapProc();
  213. portsnapUI->init(false, Version);
  214. }
  215.  
  216. void PBSystemTab::fetchPortsPressed()
  217. {
  218. portsnapUI = new PortsnapProc();
  219. portsnapUI->init(true, Version);
  220. }
  221.  
  222.  
  223. void PBSystemTab::loadBootData()
  224. {
  225. /*//Trawl screens directory
  226. QDir screens = QDir(PREFIX + "/share/pcbsd/splash-screens/");
  227. if (! screens.exists())
  228. {
  229. QMessageBox::critical( 0, tr("File not found!"), PREFIX + "/share/pcbsd/splash-screens/ " + tr("doesn't exist!"), QMessageBox::Ok );
  230. return;
  231. }
  232. screens.setFilter(QDir::Files);
  233. QStringList screensList = screens.entryList();
  234.  
  235. QRegExp screenMatcher = QRegExp("^loading-screen-(([a-z]|[A-Z]|_)+)\\.pcx$");
  236. QStringList langList = QStringList();
  237. QLocale langResolver;
  238. codeMap = QMap<QString, QString>();
  239. bool customPresent = false;
  240. for (QStringList::Iterator it = screensList.begin(); it != screensList.end(); it++)
  241. {
  242. int pos = screenMatcher.indexIn(*it);
  243. if (pos > -1)
  244. {
  245. QString code = screenMatcher.cap(1);
  246. if (code == "custom") { customPresent = true; }
  247. else
  248. {
  249. QLocale lang(code);
  250. QString langName = langResolver.languageToString(lang.language());
  251. if ( langName.isEmpty() )
  252. {
  253. langName = "Splash Screen: " + code;
  254. }
  255. langList += langName;
  256. codeMap[langName] = code;
  257. }
  258. }
  259. }
  260.  
  261.  
  262. //Populate select box
  263. langList.sort();
  264. splashSelect->addItems(langList);
  265.  
  266. int indexMod = 0;
  267.  
  268. if (customPresent)
  269. {
  270. codeMap[tr("Custom")] = "custom";
  271. splashSelect->addItem(tr("Custom"), 0);
  272. indexMod = 1;
  273. }
  274.  
  275. //Load current, get info from registry - /PCBSD/splash-screen
  276. QSettings settings;
  277. QString selectedLang = settings.value("/PCBSD/splash-screen", langResolver.language()).toString();
  278.  
  279. if (selectedLang == "custom") { splashSelect->setCurrentIndex(0); }
  280. else
  281. {
  282. //If we don't have a splash for the user's language, default to english
  283. QLocale tlocale(selectedLang);
  284. QString testLangName = langResolver.languageToString(tlocale.language());
  285. if (codeMap[testLangName] == "") selectedLang = "en";
  286.  
  287. QLocale tlocale2(selectedLang);
  288. selectedLang = langResolver.languageToString(tlocale2.language());
  289. if ( selectedLang.isEmpty() )
  290. {
  291. selectedLang = "Splash Screen: " + selectedLang;
  292. }
  293.  
  294. splashSelect->setCurrentIndex(langList.indexOf(selectedLang) + indexMod);
  295. }
  296. */
  297. if ( pcbsd::Utils::getConfFileValue(QString(PREFIX + "/share/pcbsd/xstartup/enable-ibus.sh"), QString("FORCEIBUS=") ) == QString("YES"))
  298. checkForceIbus->setChecked(TRUE);
  299. else
  300. checkForceIbus->setChecked(FALSE);
  301. }
  302.  
  303. void PBSystemTab::slotMiscSave() {
  304. saveKernScreen();
  305.  
  306. // Save package set
  307. if ( radioProduction->isChecked() )
  308. pcbsd::Utils::setValPCBSDConf("PACKAGE_SET", "PRODUCTION");
  309. else
  310. pcbsd::Utils::setValPCBSDConf("PACKAGE_SET", "EDGE");
  311.  
  312. // Extract the ports overlay to grab a new package set repo config
  313. QProcess::execute(QString("pc-extractoverlay"), QStringList() << "ports");
  314. }
  315.  
  316. void PBSystemTab::slotClose() {
  317. close();
  318. }
  319.  
  320. bool PBSystemTab::sanityCheckSettings()
  321. {
  322. return true;
  323. }
  324.  
  325.  
  326. QString PBSystemTab::getLineFromCommandOutput( QString cmd )
  327. {
  328. FILE *file = popen(cmd.toLatin1(),"r");
  329.  
  330. char buffer[100];
  331.  
  332. QString line = "";
  333. char firstChar;
  334.  
  335. if ((firstChar = fgetc(file)) != -1){
  336. line += firstChar;
  337. line += fgets(buffer,100,file);
  338. }
  339. pclose(file);
  340. return line.simplified();
  341. }
  342.  
  343. void PBSystemTab::changeOpenTab(int tab)
  344. {
  345. tabWidget->setCurrentIndex(tab);
  346. }
  347.  
  348. void PBSystemTab::checkProxy()
  349. {
  350. bool ok;
  351. int port;
  352.  
  353. // If no proxy set
  354. if ( pcbsd::Utils::getProxyURL().isEmpty() )
  355. return;
  356.  
  357. QNetworkProxy proxy;
  358. if ( pcbsd::Utils::getProxyType() == "SOCKS5" )
  359. proxy.setType(QNetworkProxy::Socks5Proxy);
  360. else
  361. proxy.setType(QNetworkProxy::HttpProxy);
  362.  
  363. proxy.setHostName(pcbsd::Utils::getProxyURL());
  364.  
  365. port = pcbsd::Utils::getProxyPort().toInt(&ok);
  366. if ( ! pcbsd::Utils::getProxyPort().isEmpty() && ok )
  367. proxy.setPort(port);
  368. if ( ! pcbsd::Utils::getProxyUser().isEmpty() )
  369. proxy.setUser(pcbsd::Utils::getProxyUser());
  370. if ( ! pcbsd::Utils::getProxyPass().isEmpty() )
  371. proxy.setPassword(pcbsd::Utils::getProxyPass());
  372.  
  373. QNetworkProxy::setApplicationProxy(proxy);
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement