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

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 0b12758 was 0b12758, checked in by Ken Moore <ken@…>, 11 months ago

Test fix for compile warnings with "out.simplified()" on 10.

  • Property mode set to 100644
File size: 5.2 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
35class Extras{
36
37public:
38  static bool checkUser(bool wardenMode){
39    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
40    QString logname;
41    if(env.contains("LOGNAME")){ logname = env.value("LOGNAME"); }
42    else if(env.contains("USERNAME")){ logname = env.value("USERNAME"); }
43    else if(env.contains("USER")){ logname = env.value("USER"); }
44    bool ok = FALSE;
45    if( logname.isEmpty() ){}
46    else if( wardenMode && (logname=="root") ){ ok = TRUE;}
47    else if( !wardenMode && (logname!="root") ){ ok = TRUE; }
48    return ok;
49  }
50 
51  static QString bytesToHumanReadable(float bytes){
52    QStringList suffix;
53    suffix << "B" << "KB" << "MB" << "GB" << "TB" << "PB";
54    int i = 0;
55    while((bytes >= 1000) && ( i < (suffix.size() - 1 )) ){
56        bytes = bytes / 1024;  i++;
57    }
58    QString result = QString::number(bytes, 'f', 0);
59    result += suffix[i];
60    return result;
61  }
62 
63  static QString getLineFromCommandOutput( QString command ){
64        FILE *file = popen(command.toLatin1(),"r"); 
65        char buffer[100];
66        QString line = ""; 
67        char firstChar;
68        if ((firstChar = fgetc(file)) != -1){
69                line += firstChar;
70                line += fgets(buffer,100,file);
71        }
72        pclose(file);
73        return line;
74  }
75 
76 static QString getSystemArch(){
77    return getLineFromCommandOutput("uname -m").simplified();
78 }
79 
80 static QStringList readFile( QString filePath ){
81   QFile file(filePath);
82   QStringList output;
83   if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
84     QTextStream in(&file);
85     while(!in.atEnd()){
86       QString line = in.readLine();
87       if(!line.isEmpty()){ output << line; }
88     }
89     file.close();
90   }
91   return output;
92 }
93 
94 static bool writeFile( QString filePath, QStringList contents ){
95   QFile file(filePath);
96   bool ok = FALSE;
97   if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
98     QTextStream out(&file);
99     for(int i=0; i<contents.length(); i++){
100       out << contents[i] + "\n";           
101     }
102     file.close();
103     ok = TRUE;
104   }
105   return ok;
106 }
107 
108 static QString nameToID(QString name){
109   QString out = name.toLower();
110   out.remove(" ");
111   out.remove("\t");
112   out = out.simplified();
113   return out;
114 }
115 
116 static bool newerDateTime(QString check, QString standard){
117   //Returns true if the first input is a later date/time than the second
118   double chkNum = check.remove(" ").toDouble();
119   double stdNum = standard.remove(" ").toDouble();
120   return (chkNum > stdNum);
121 }
122 
123 static QString datetimeToString(QString datetime){
124   //datetime format: "YYYYMMDD HHMMSS"
125   //converts a database date/time string to human readable form
126   QString date = datetime.section(" ",0,0,QString::SectionSkipEmpty);
127   //date format: YYYYMMDD
128   QString year = date.left(4);
129   QString day = QString::number(date.right(2).toInt());
130   date.chop(2); // remove the day
131   QString month = QString::number(date.right(2).toInt());
132   
133   QString output = month+"/"+day+"/"+year;
134   return output;
135 }
136 
137 static QString sizeKToDisplay(QString sizeK){
138   double num = sizeK.toDouble();
139   QStringList lab; lab << "KB" << "MB" << "GB" << "TB" << "PB";
140   int i=0;
141   while( (i<lab.length()) && (num > 1024) ){
142     num=num/1024; i++;   
143   }
144   //Round to 2 decimel places
145   num = int(num*100)/100.0;
146   QString output = QString::number(num)+" "+lab[i];
147   //qDebug() << "Size calculation:" << sizeK << output;
148   return output;
149 }
150 
151};
152
153#endif
Note: See TracBrowser for help on using the repository browser.