1 | /*************************************************************************** |
2 | commandengine.cpp - kfr commands feature class |
3 | ------------------- |
4 | begin : fri aug 13 15:29:46 CEST 2004 |
5 | |
6 | copyright : (C) 2004 Emiliano Gulmini |
7 | email : emi_barbarossa@yahoo.it |
8 | ***************************************************************************/ |
9 | |
10 | /*************************************************************************** |
11 | * * |
12 | * This program is free software; you can redistribute it and/or modify * |
13 | * it under the terms of the GNU General Public License as published by * |
14 | * the Free Software Foundation; either version 2 of the License, or * |
15 | * (at your option) any later version. * |
16 | * * |
17 | ***************************************************************************/ |
18 | |
19 | // QT |
20 | #include <qdatetime.h> |
21 | #include <qfile.h> |
22 | #include <qtextstream.h> |
23 | #include <qdom.h> |
24 | //Added by qt3to4: |
25 | #include <Q3CString> |
26 | |
27 | // KDE |
28 | #include <kuser.h> |
29 | #include <krandomsequence.h> |
30 | #include <k3process.h> |
31 | |
32 | // local |
33 | #include "commandengine.h" |
34 | |
35 | QString CommandEngine::datetime(const QString& opt, const QString& arg) |
36 | { |
37 | Q_UNUSED(arg); |
38 | if(opt == "iso" ) |
39 | return QDateTime::currentDateTime().toString(Qt::ISODate); |
40 | if(opt == "local" ) |
41 | return QDateTime::currentDateTime().toString(Qt::LocalDate); |
42 | return QString(); |
43 | } |
44 | |
45 | QString CommandEngine::user(const QString& opt, const QString& arg) |
46 | { |
47 | Q_UNUSED(arg); |
48 | KUser u; |
49 | if(opt == "uid" ) |
50 | return QString::number(u.uid(),10); |
51 | if(opt == "gid" ) |
52 | return QString::number(u.gid(),10); |
53 | if(opt == "loginname" ) |
54 | return u.loginName(); |
55 | if(opt == "fullname" ) |
56 | return u.fullName(); |
57 | if(opt == "homedir" ) |
58 | return u.homeDir(); |
59 | if(opt == "shell" ) |
60 | return u.shell(); |
61 | return QString(); |
62 | } |
63 | |
64 | QString CommandEngine::loadfile(const QString& opt, const QString& arg) |
65 | { |
66 | Q_UNUSED(arg); |
67 | |
68 | QFile f(opt); |
69 | if(!f.open(QIODevice::ReadOnly)) return QString(); |
70 | |
71 | QTextStream t(&f); |
72 | |
73 | QString s = t.readAll(); |
74 | |
75 | f.close(); |
76 | |
77 | return s; |
78 | } |
79 | |
80 | QString CommandEngine::empty(const QString& opt, const QString& arg) |
81 | { |
82 | Q_UNUSED(opt); |
83 | Q_UNUSED(arg); |
84 | return "" ; |
85 | } |
86 | |
87 | QString CommandEngine::mathexp(const QString& opt, const QString& arg) |
88 | { |
89 | /* We will use bc 1.06 by Philip A. Nelson <philnelson@acm.org> */ |
90 | //Q_UNUSED(opt); |
91 | Q_UNUSED(arg); |
92 | |
93 | QString tempOpt = opt; |
94 | tempOpt.replace("ln" ,"l" ); |
95 | tempOpt.replace("sin" ,"s" ); |
96 | tempOpt.replace("cos" ,"c" ); |
97 | tempOpt.replace("arctan" ,"a" ); |
98 | tempOpt.replace("exp" ,"e" ); |
99 | |
100 | QString program = "var=(" +tempOpt+");print var" ; |
101 | QString script = "echo '" +program+"' | bc -l;" ; |
102 | |
103 | K3Process* proc = new K3Process(); |
104 | |
105 | proc->setUseShell(true); |
106 | |
107 | *(proc) << script; |
108 | |
109 | connect(proc, SIGNAL(receivedStdout(K3Process*,char*,int)), this, SLOT(slotGetScriptOutput(K3Process*,char*,int))); |
110 | connect(proc, SIGNAL(receivedStderr(K3Process*,char*,int)), this, SLOT(slotGetScriptError(K3Process*,char*,int))); |
111 | connect(proc, SIGNAL(processExited(K3Process*)), this, SLOT(slotProcessExited(K3Process*))); |
112 | |
113 | //Through slotGetScriptOutput, m_processOutput contains the result of the K3Process call |
114 | if(!proc->start(K3Process::Block, K3Process::All)) |
115 | { |
116 | return QString(); |
117 | } |
118 | else |
119 | { |
120 | proc->wait(); |
121 | } |
122 | delete proc; |
123 | |
124 | QString tempbuf = m_processOutput; |
125 | m_processOutput = QString(); |
126 | |
127 | return tempbuf; |
128 | |
129 | } |
130 | |
131 | QString CommandEngine::random(const QString& opt, const QString& arg) |
132 | { |
133 | Q_UNUSED(arg); |
134 | long seed; |
135 | if(opt.isEmpty()) |
136 | { |
137 | QDateTime dt; |
138 | seed = dt.toTime_t(); |
139 | } |
140 | else |
141 | seed = opt.toLong(); |
142 | |
143 | KRandomSequence seq(seed); |
144 | return QString::number(seq.getLong(1000000),10); |
145 | } |
146 | |
147 | QString CommandEngine::stringmanip(const QString& opt, const QString& arg) |
148 | { |
149 | Q_UNUSED(opt); |
150 | Q_UNUSED(arg); |
151 | return "" ; |
152 | } |
153 | |
154 | QString CommandEngine::variableValue(const QString &variable) |
155 | { |
156 | QString s = variable; |
157 | |
158 | s.remove("[$" ).remove("$]" ).remove(" " ); |
159 | |
160 | if(s.contains(":" ) == 0) |
161 | return variable; |
162 | else |
163 | { |
164 | QString leftValue = s.section(":" ,0,0), |
165 | midValue = s.section(":" ,1,1), |
166 | rightValue = s.section(":" ,2,2); |
167 | |
168 | QString opt = midValue; |
169 | QString arg = rightValue; |
170 | |
171 | if(leftValue == "stringmanip" ) |
172 | return stringmanip(opt, arg); |
173 | if(leftValue == "datetime" ) |
174 | return datetime(opt, arg); |
175 | if(leftValue == "user" ) |
176 | return user(opt, arg); |
177 | if(leftValue == "loadfile" ) |
178 | return loadfile(opt, arg); |
179 | if(leftValue == "empty" ) |
180 | return empty(opt, arg); |
181 | if(leftValue == "mathexp" ) |
182 | return mathexp(opt, arg); |
183 | if(leftValue == "random" ) |
184 | return random(opt, arg); |
185 | |
186 | return variable; |
187 | } |
188 | } |
189 | |
190 | //SLOTS |
191 | void CommandEngine::slotGetScriptError(K3Process* proc, char* s, int i) |
192 | { |
193 | Q_UNUSED(proc); |
194 | Q_UNUSED(proc); |
195 | Q3CString temp(s,i+1); |
196 | if(temp.isEmpty() || temp == "\n" ) return; |
197 | } |
198 | |
199 | void CommandEngine::slotGetScriptOutput(K3Process* proc, char* s, int i) |
200 | { |
201 | Q_UNUSED(proc); |
202 | Q3CString temp(s,i+1); |
203 | |
204 | if(temp.isEmpty() || temp == "\n" ) return; |
205 | |
206 | m_processOutput += QString::fromLocal8Bit(temp); |
207 | } |
208 | |
209 | void CommandEngine::slotProcessExited(K3Process* proc) |
210 | { |
211 | Q_UNUSED(proc); |
212 | } |
213 | |
214 | #include "commandengine.moc" |
215 | |