summaryrefslogtreecommitdiffstats
path: root/src/magiclabel.cpp
blob: 57d50c7ba6514f2185712bea120c7eb23cfa4b84 (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
/***************************************************************************
 *   Copyright (C) by                                                      *
 *     - 2005: Christian Leh <moodwrod@web.de>                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqstringlist.h>
#include <tqregexp.h>

#include <klocale.h>
#include <kuser.h>
#include <kmessagebox.h>

#include "magiclabel.h"

// This class is still very simple (as most of the classes)
// They get all improved later when "OpenGL Effect Widget" structure is completed

MagicLabel::MagicLabel(TQString s, bool translate)
{
  prefix = "ML:";
  preUSER = "USER:";
  preCMD = "CMD:";
  mValue = s;

  transform();

  if (translate)
    mValue = i18n(mValue);
}


void MagicLabel::transform()
{
  if (mValue.contains(prefix + preUSER))
    getUserInfo();
  else if (mValue.startsWith(prefix + preCMD))
    getCommandOutput();
}


void MagicLabel::getUserInfo()
{
  static KUser user;

  if (mValue.contains(prefix + preUSER + "loginname"))
    mValue = mValue.replace(prefix + preUSER + "loginname", user.loginName());
  else if (mValue.contains(prefix + preUSER + "fullname"))
    mValue = mValue.replace(prefix + preUSER + "fullname", user.fullName());
  else if (mValue.contains(prefix + preUSER + "homedir"))
    mValue = mValue.replace(prefix + preUSER + "homedir", user.homeDir());
}


void MagicLabel::getCommandOutput()
{
  TQString cmd = TQStringList::split(prefix + preCMD, mValue)[0];
  TQStringList parts = TQStringList::split(" ", cmd);

  KShellProcess *proc = new KShellProcess;

  for (int i = 0; i < parts.count(); i++)
    *proc << parts[i];

  connect(proc, TQT_SIGNAL(processExited(KProcess*)), this, TQT_SLOT(processExited(KProcess*)));
  connect(proc, TQT_SIGNAL(receivedStdout(KProcess*, char*, int)), this, TQT_SLOT(receivedStdout(KProcess*, char*, int)));

  mValue = "";

  if (!proc->start(KProcess::Block, KProcess::Stdout))
    KMessageBox::information(0, TQString("Could not start process: %1").arg(cmd));
}


void MagicLabel::receivedStdout(KProcess *proc, char *buffer, int buflen)
{
  TQString buf = TQString::fromLatin1(buffer, buflen);
  mValue += buf.replace("\n", "");
}


void MagicLabel::processExited(KProcess* proc)
{
  delete proc;
}

#include "magiclabel.moc"