summaryrefslogtreecommitdiffstats
path: root/klinkstatus/src/global.cpp
blob: 9fe9d96d5bc770b820e8bbc9fe3818219e3864a7 (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
//
// C++ Implementation: global
//
// Description:
//
//
// Author: Paulo Moura Guedes <[email protected]>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "global.h"

#include <tqstring.h>
#include <tqtimer.h>

#include <dcopclient.h>
#include <dcopref.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kstaticdeleter.h>
#include <kurl.h>
#include <kprocess.h>

#include <sys/types.h>
#include <unistd.h>


Global* Global::m_self_ = 0;
static KStaticDeleter<Global> staticDeleter;


Global* Global::self()
{
    if (!m_self_)
    {
        staticDeleter.setObject(m_self_, new Global());
    }

    return m_self_;
}

Global::Global(TQObject *tqparent, const char *name)
        : TQObject(tqparent, name), loop_started_(false)
{
    m_self_ = this;
    dcop_client_ = kapp->dcopClient();
}

Global::~Global()
{
    if(m_self_ == this)
        staticDeleter.setObject(m_self_, 0, false);
}

bool Global::isKLinkStatusEmbeddedInQuanta()
{
    TQCString app_id = "quanta-" + TQCString().setNum(getpid());
    return self()->dcop_client_->isApplicationRegistered(app_id);
}

bool Global::isQuantaRunningAsUnique()
{
    return self()->dcop_client_->isApplicationRegistered("quanta");
}

bool Global::isQuantaAvailableViaDCOP()
{
    if(isQuantaRunningAsUnique() || isKLinkStatusEmbeddedInQuanta())
        return true;

    else
    {
        self()->execCommand("ps h -o pid -C quanta -C quanta_be");
        TQStringList ps_list = TQStringList::split("\n", self()->script_output_);

        for(uint i = 0; i != ps_list.size(); ++i)
        {
            ps_list[i] = ps_list[i].stripWhiteSpace ();
            if(self()->dcop_client_->isApplicationRegistered("quanta-" + ps_list[i].local8Bit()))
            {
                //kdDebug(23100) << "Application registered!" << endl;
                return true;
            }
        }
        return false;
    }
}

TQCString Global::quantaDCOPAppId()
{
    DCOPClient* client = kapp->dcopClient();
    TQCString app_id;

    if(client->isApplicationRegistered("quanta")) // quanta is unnique application
        app_id = "quanta";

    else if(self()->isKLinkStatusEmbeddedInQuanta()) // klinkstatus is running as a part inside quanta
    {
        TQCString app = "quanta-";
        TQCString pid = TQCString().setNum(getpid());
        app_id = app + pid;
    }

    else
    {
        self()->execCommand("ps h -o pid -C quanta -C quanta_be");
        TQStringList ps_list = TQStringList::split("\n", self()->script_output_);

        for(uint i = 0; i != ps_list.size(); ++i)
        {
            ps_list[i] = ps_list[i].stripWhiteSpace ();
            if(self()->dcop_client_->isApplicationRegistered("quanta-" + ps_list[i].local8Bit()))
                app_id = "quanta-" + ps_list[i];
        }
    }

    if(self()->dcop_client_->isApplicationRegistered(app_id))
        return app_id;
    else
    {
        kdError(23100) << "You didn't check if Global::isQuantaAvailableViaDCOP!" << endl;
        return "";
    }
}

KURL Global::urlWithQuantaPreviewPrefix(KURL const& url)
{
    Q_ASSERT(isKLinkStatusEmbeddedInQuanta());

    DCOPRef quanta(Global::quantaDCOPAppId(),"WindowManagerIf");
    TQString string_url_with_prefix = quanta.call("urlWithPreviewPrefix", url.url());
    //kdDebug(23100) << "string_url_with_prefix: " << string_url_with_prefix << endl;

    return KURL(string_url_with_prefix);
}

void Global::openQuanta(TQStringList const& args)
{
    TQString command(args.join(" "));
    Global::execCommand("quanta " + command);    
}

void Global::execCommand(TQString const& command)
{

    //We create a KProcess that executes the "ps" *nix command to get the PIDs of the
    //other instances of quanta actually running
    self()->process_PS_ = new KProcess();
    *(self()->process_PS_) << TQStringList::split(" ",command);

    connect( self()->process_PS_, TQT_SIGNAL(receivedStdout(KProcess*,char*,int)),
             self(), TQT_SLOT(slotGetScriptOutput(KProcess*,char*,int)));
    connect( self()->process_PS_, TQT_SIGNAL(receivedStderr(KProcess*,char*,int)),
             self(), TQT_SLOT(slotGetScriptError(KProcess*,char*,int)));
    connect( self()->process_PS_, TQT_SIGNAL(processExited(KProcess*)),
             self(), TQT_SLOT(slotProcessExited(KProcess*)));

    //if KProcess fails I think a message box is needed... I will fix it
    if (!self()->process_PS_->start(KProcess::NotifyOnExit,KProcess::All))
        kdError() << "Failed to query for running KLinkStatus instances!" << endl;
    //TODO: Replace the above error with a real messagebox after the message freeze is over
    else
    {
        //To avoid lock-ups, start a timer.
        TQTimer* timer = new TQTimer(self());
        connect(timer, TQT_SIGNAL(timeout()),
                self(), TQT_SLOT(slotProcessTimeout()));
        timer->start(120*1000, true);
        self()->loop_started_ = true;
        kapp->enter_loop();
        delete timer;
    }
}

void Global::slotGetScriptOutput(KProcess* /*process*/, char* buf, int buflen)
{
    TQCString tmp( buf, buflen + 1 );
    script_output_ = TQString();
    script_output_ = TQString::fromLocal8Bit(tmp).remove(" ");
}

void Global::slotGetScriptError(KProcess*, char* buf, int buflen)
{
    //TODO: Implement some error handling?
    Q_UNUSED(buf);
    Q_UNUSED(buflen);
}

void Global::slotProcessExited(KProcess*)
{
    slotProcessTimeout();
}

void Global::slotProcessTimeout()
{
    if (loop_started_)
    {
        kapp->exit_loop();
        loop_started_ = false;
    }
}


#include "global.moc"