source: src-qt4/pc-softwaremanager/extras.h @ e543bca

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since e543bca was e543bca, checked in by Ken Moore <ken@…>, 10 months ago

Add the ability for the AppCafe? to install random PBI files on the system through a menu option, and update the Installed tab interface a bit to make it more uniform between the two pages. Still have a couple things to do to finish it up, but it is perfectly functional.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2011 - iXsystems                                       *
3 *   kris@pcbsd.org  *
4 *   tim@pcbsd.org   *
5 *   ken@pcbsd.org   *
6 *                                                                         *
7 *   Permission is hereby granted, free of charge, to any person obtaining *
8 *   a copy of this software and associated documentation files (the       *
9 *   "Software"), to deal in the Software without restriction, including   *
10 *   without limitation the rights to use, copy, modify, merge, publish,   *
11 *   distribute, sublicense, and/or sell copies of the Software, and to    *
12 *   permit persons to whom the Software is furnished to do so, subject to *
13 *   the following conditions:                                             *
14 *                                                                         *
15 *   The above copyright notice and this permission notice shall be        *
16 *   included in all copies or substantial portions of the Software.       *
17 *                                                                         *
18 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
19 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
20 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
22 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
23 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
24 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
25 ***************************************************************************/
26#ifndef _APPCAFE_EXTRAS_H
27#define _APPCAFE_EXTRAS_H
28
29#include <QString>
30#include <QFile>
31#include <QTextStream>
32#include <QDebug>
33#include <QProcessEnvironment>
34#include <QProcess>
35#include <QCoreApplication>
36
37class Extras{
38
39public:
40  static bool checkUser(bool wardenMode){
41    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
42    QString logname;
43    if(env.contains("LOGNAME")){ logname = env.value("LOGNAME"); }
44    else if(env.contains("USERNAME")){ logname = env.value("USERNAME"); }
45    else if(env.contains("USER")){ logname = env.value("USER"); }
46    bool ok = FALSE;
47    if( logname.isEmpty() ){}
48    else if( wardenMode && (logname=="root") ){ ok = TRUE;}
49    else if( !wardenMode && (logname!="root") ){ ok = TRUE; }
50    return ok;
51  }
52 
53  static QString bytesToHumanReadable(float bytes){
54    QStringList suffix;
55    suffix << "B" << "KB" << "MB" << "GB" << "TB" << "PB";
56    int i = 0;
57    while((bytes >= 1000) && ( i < (suffix.size() - 1 )) ){
58        bytes = bytes / 1024;  i++;
59    }
60    QString result = QString::number(bytes, 'f', 0);
61    result += suffix[i];
62    return result;
63  }
64 
65  static QString getLineFromCommandOutput( QString command ){
66        FILE *file = popen(command.toLatin1(),"r"); 
67        char buffer[100];
68        QString line = ""; 
69        char firstChar;
70        if ((firstChar = fgetc(file)) != -1){
71                line += firstChar;
72                line += fgets(buffer,100,file);
73        }
74        pclose(file);
75        return line;
76  }
77 
78 static QString getSystemArch(){
79    return getLineFromCommandOutput("uname -m").simplified();
80 }
81 
82 static QStringList readFile( QString filePath ){
83   QFile file(filePath);
84   QStringList output;
85   if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
86     QTextStream in(&file);
87     while(!in.atEnd()){
88       QString line = in.readLine();
89       if(!line.isEmpty()){ output << line; }
90     }
91     file.close();
92   }
93   return output;
94 }
95 
96 static bool writeFile( QString filePath, QStringList contents ){
97   QFile file(filePath);
98   bool ok = FALSE;
99   if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
100     QTextStream out(&file);
101     for(int i=0; i<contents.length(); i++){
102       out << contents[i] + "\n";           
103     }
104     file.close();
105     ok = TRUE;
106   }
107   return ok;
108 }
109 
110 static QString nameToID(QString name){
111   QString out = name.toLower();
112   out.remove(" ");
113   out.remove("\t");
114   out = out.simplified();
115   return out;
116 }
117 
118 static bool newerDateTime(QString check, QString standard){
119   //Returns true if the first input is a later date/time than the second
120   double chkNum = check.remove(" ").toDouble();
121   double stdNum = standard.remove(" ").toDouble();
122   return (chkNum > stdNum);
123 }
124 
125 static QString datetimeToString(QString datetime){
126   //datetime format: "YYYYMMDD HHMMSS"
127   //converts a database date/time string to human readable form
128   QString date = datetime.section(" ",0,0,QString::SectionSkipEmpty);
129   //date format: YYYYMMDD
130   QString year = date.left(4);
131   QString day = QString::number(date.right(2).toInt());
132   date.chop(2); // remove the day
133   QString month = QString::number(date.right(2).toInt());
134   
135   QString output = month+"/"+day+"/"+year;
136   return output;
137 }
138 
139 static QString sizeKToDisplay(QString sizeK){
140   double num = sizeK.toDouble();
141   QStringList lab; lab << "KB" << "MB" << "GB" << "TB" << "PB";
142   int i=0;
143   while( (i<lab.length()) && (num > 1024) ){
144     num=num/1024; i++;   
145   }
146   //Round to 2 decimel places
147   num = int(num*100)/100.0;
148   QString output = QString::number(num)+" "+lab[i];
149   //qDebug() << "Size calculation:" << sizeK << output;
150   return output;
151 }
152 
153static QStringList getCmdOutput(QString cmd, QString dir = ""){
154  QProcess *proc = new QProcess;
155  proc->setProcessChannelMode(QProcess::MergedChannels);
156  if( !dir.isEmpty() && QFile::exists(dir) ){ proc->setWorkingDirectory(dir); }
157  proc->start(cmd);
158  while(!proc->waitForFinished(300)){
159    QCoreApplication::processEvents();
160  }
161  QStringList out = QString(proc->readAllStandardOutput()).split("\n"); 
162  delete proc; 
163  return out;
164}
165 
166};
167
168#endif
Note: See TracBrowser for help on using the repository browser.