summaryrefslogtreecommitdiff
path: root/backend.cpp
blob: c415f5387c336611a6fcb33ba3897969a5c2efa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "backend.h"
#include <QDebug>
#include <QTime>
#include <stdlib.h>
#include <math.h>
#include <crypt.h>
#include "config.h"

Backend* Backend::_instance;

Backend::Backend()
{
  qDebug() << "Backend::Backend() called!";
  _process = new QProcess();
  
  QTime time = QTime::currentTime();
  srand((uint)time.msec());
  connect(_process, SIGNAL(finished(int)), this, SLOT(slotProcessExited()));
  connect(_process, SIGNAL(readyReadStandardOutput()), this, SLOT(slotReceiveOutput()));
}

Backend::~Backend()
{
}

void Backend::runBackend()
{
  QStringList command = QStringList() << BACKEND_PATH << "-ni";
  _process->start(command.join(" "));
  busy = true; emit isBusy(true);
}

bool Backend::isBusy()
{
  return (busy && commandQueue.isEmpty());
}

bool Backend::flag(QString flag)
{
  return flags.contains(flag);
}

void Backend::_flag(QString flag, bool set)
{
  if(set)
  {
    if(!flags.contains(flag))
      flags.append(flag);
  }
  else
  {
    if(flags.contains(flag))
      flags.remove(flags.indexOf(flag));
  }
}

void Backend::flag(QString flag, bool set)
{
  if(set)
  {
    exec("flag_set '"+flag.replace("'","\\'")+"'");
    _flag(flag, true);
  }
  else
  {
    exec("flag_unset '"+flag.replace("'","\\'")+"'");
    _flag(flag, false);
  }
}

QString Backend::cfg(QString var)
{
  return cfgMap.value(var);
}

void Backend::_cfg(QString var, QString value)
{
  cfgMap[var] = value;
}

void Backend::cfg(QString var, QString value)
{
  exec("cfg_set '"+var.replace("'","\\'")+"' '"+value.replace("'","\\'")+"'");
  _cfg(var, value);
}

void Backend::exec(QString command)
{
  qDebug() << "Backend::exec(\"" << command.toAscii() << "\") called!";
  commandQueue.enqueue(command);
  dequeue();
}

void Backend::dequeue()
{
  if(!busy && commandQueue.isEmpty()) emit isBusy(false);
  if(busy) return;
  if(commandQueue.isEmpty()) return;
  busy = true; emit isBusy(true);
  QString command = commandQueue.dequeue();
  currentCommand = command;
  data.clear();
  _process->write(command.append('\004').toLocal8Bit());
}

void Backend::exitBackend()
{
  qDebug() << "Backend::exitBackend() called!";
  _process->kill();
  _process->waitForFinished();
}

void Backend::processOutput(QString line)
{
  qDebug() << "Backend: input:" << line.toAscii();
  if(line.startsWith("<acritoxinstaller "))
  {
    QString command = line.section(" ",1,1).section(">",0,0);
    QString args = line.section(" ",2).trimmed();
    if(command == "prompt")
    {
      busy = false;
      qDebug() << "Backend: finished:" << currentCommand.toAscii();
      emit finishedCommand(currentCommand);
      dequeue();
    }
    else if(command == "flag")
    {
      QString setunset = args.section(" ",0,0);
      QString flag = args.section(" ",1).trimmed();
      if(setunset == "set")
	_flag(flag, true);
      else if(setunset == "unset")
	_flag(flag, false);
    }
    else if(command == "data")
    {
      currentData = args.trimmed();
      data[currentData] = "";
    }
    else if(command == "cfg")
    {
      _cfg(args.trimmed(), data["value"].trimmed());
    }
    else if(command == "progress")
    {
      emit receivedProgress(args.toInt());
    }
    else
      emit receivedCommand(command, args);
  }
  else
  {
    data[currentData].append(line);
    emit receivedDataLine(currentData, line.trimmed());
    qDebug() << "Backend: data:" << line.toAscii();
  }
}

QString Backend::encryptPassword(QString password)
{
  unsigned long seed[2];
  char salt[] = "$1$........";
  char *pwd = password.toAscii().data();
  const char *const seedchars =
    "./0123456789ABCDEFGHIJKLMNOPQRST"
    "UVWXYZabcdefghijklmnopqrstuvwxyz";
  int i;
  
  seed[0] = rand(); seed[1] = rand();
  
  for (i = 0; i < 8; i++)
    salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
    
  return(QString(crypt(pwd, salt)));
}

// function adapted from amarok 2.0 / App.cpp - (C) 2002 by Mark Kretschmann <markey@web.de>
QString Backend::cleanUsername(const QString &username)
{
  QString result = username;
  
  // german umlauts
  result.replace( QChar(0x00e4), "ae" ).replace( QChar(0x00c4), "Ae" );
  result.replace( QChar(0x00f6), "oe" ).replace( QChar(0x00d6), "Oe" );
  result.replace( QChar(0x00fc), "ue" ).replace( QChar(0x00dc), "Ue" );
  result.replace( QChar(0x00df), "ss" );

  // some strange accents
  result.replace( QChar(0x00e7), "c" ).replace( QChar(0x00c7), "C" );
  result.replace( QChar(0x00fd), "y" ).replace( QChar(0x00dd), "Y" );
  result.replace( QChar(0x00f1), "n" ).replace( QChar(0x00d1), "N" );

  // czech letters with carons
  result.replace( QChar(0x0161), "s" ).replace( QChar(0x0160), "S" );
  result.replace( QChar(0x010d), "c" ).replace( QChar(0x010c), "C" );
  result.replace( QChar(0x0159), "r" ).replace( QChar(0x0158), "R" );
  result.replace( QChar(0x017e), "z" ).replace( QChar(0x017d), "Z" );
  result.replace( QChar(0x0165), "t" ).replace( QChar(0x0164), "T" );
  result.replace( QChar(0x0148), "n" ).replace( QChar(0x0147), "N" );
  result.replace( QChar(0x010f), "d" ).replace( QChar(0x010e), "D" );

  // accented vowels
  QChar a[] = { 'a', 0xe0,0xe1,0xe2,0xe3,0xe5, 0 };
  QChar A[] = { 'A', 0xc0,0xc1,0xc2,0xc3,0xc5, 0 };
  QChar E[] = { 'e', 0xe8,0xe9,0xea,0xeb,0x11a, 0 };
  QChar e[] = { 'E', 0xc8,0xc9,0xca,0xcb,0x11b, 0 };
  QChar i[] = { 'i', 0xec,0xed,0xee,0xef, 0 };
  QChar I[] = { 'I', 0xcc,0xcd,0xce,0xcf, 0 };
  QChar o[] = { 'o', 0xf2,0xf3,0xf4,0xf5,0xf8, 0 };
  QChar O[] = { 'O', 0xd2,0xd3,0xd4,0xd5,0xd8, 0 };
  QChar u[] = { 'u', 0xf9,0xfa,0xfb,0x16e, 0 };
  QChar U[] = { 'U', 0xd9,0xda,0xdb,0x16f, 0 };
  QChar nul[] = { 0 };
  QChar *replacements[] = { a, A, e, E, i, I, o, O, u, U, nul };

  for( int i = 0; i < result.length(); i++ )
  {
    QChar c = result[ i ];
    for( uint n = 0; replacements[n][0] != QChar(0); n++ )
    {
      for( uint k=0; replacements[n][k] != QChar(0); k++ )
      {
        if( replacements[n][k] == c )
        {
          c = replacements[n][0];
        }
      }
    }
    result[ i ] = c;
  }
  
  return result.replace(QRegExp("[^a-zA-Z0-9-_.]"), "");
}

QString Backend::sizeToString(qlonglong size)
{
  float fSize = size;
  int i = 0;
  const char* units[] = {"B", "kB", "MB", "GB", "TB"};
  while (fSize > 1024) {
    fSize /= 1024.0;
    i++;
  }
  fSize = round((fSize)*100)/100;
  return QString::number(fSize)+" "+ units[i];
}

void Backend::slotProcessExited()
{
  emit processExited();
}

void Backend::slotReceiveOutput()
{
  _process->setReadChannel(QProcess::StandardOutput);
  char data[2048]; QString line;
  while(_process->canReadLine())
  {
    int result = _process->readLine(data, sizeof(data));
    if(result == -1) break;
    line = QString::fromLocal8Bit(data);
    processOutput(line);
  }
}