summaryrefslogtreecommitdiff
path: root/library/qcopenvelope_qws.cpp
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /library/qcopenvelope_qws.cpp
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'library/qcopenvelope_qws.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/qcopenvelope_qws.cpp162
1 files changed, 162 insertions, 0 deletions
diff --git a/library/qcopenvelope_qws.cpp b/library/qcopenvelope_qws.cpp
new file mode 100644
index 0000000..10d1567
--- a/dev/null
+++ b/library/qcopenvelope_qws.cpp
@@ -0,0 +1,162 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "qcopenvelope_qws.h"
22#include "global.h"
23#include <qbuffer.h>
24#include <qdatastream.h>
25#include <qfile.h>
26#include <unistd.h>
27#include <errno.h>
28#include <sys/file.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <time.h>
32
33#ifndef QT_NO_COP
34
35/*!
36 \class QCopEnvelope qcopenvelope_qws.h
37 \brief The QCopEnvelope class encapsulates QCop message sending.
38
39 QCop messages allow applications to communicate with each other.
40 These messages are send using QCopEnvelope, and received by connecting
41 to a QCopChannel.
42
43 To send a message, use the following protocol:
44
45 \code
46 QCopEnvelope e(channelname, messagename);
47 e << parameter1 << parameter2 << ...;
48 \endcode
49
50 For messages without parameters, you can simply use:
51
52 \code
53 QCopEnvelope (channelname, messagename);
54 \endcode
55
56 Do not try to simplify further as some compilers may not do
57 as you expect.
58
59 The <tt>channelname</tt> of channels within Qtopia all start with "QPE/".
60 The <tt>messagename</tt> is a function identifier followed by a list of types
61 in parentheses. There are no spaces in the message name.
62
63 To receive a message, you will generally just use your applications
64 predefined QPE/Application/<i>appname</i> channel
65 (see QPEApplication::appMessage()), but you can make another channel
66 and connect it to a slot with:
67
68 \code
69 myChannel = new QCopChannel( "QPE/FooBar", this );
70 connect( myChannel, SIGNAL(received(const QCString &, const QByteArray &)),
71 this, SLOT(fooBarMessage( const QCString &, const QByteArray &)) );
72 \endcode
73
74 See also, the \link qcop.html list of Qtopia messages\endlink.
75*/
76
77/*!
78 Constructs a QCopEnvelope that will write \a message to \a channel.
79 If \a message has parameters, you must then user operator<<() to
80 write the parameters.
81*/
82QCopEnvelope::QCopEnvelope( const QCString& channel, const QCString& message ) :
83 QDataStream(new QBuffer),
84 ch(channel), msg(message)
85{
86 device()->open(IO_WriteOnly);
87}
88
89/*!
90 Writes the completed message and destroys the QCopEnvelope.
91*/
92QCopEnvelope::~QCopEnvelope()
93{
94 QByteArray data = ((QBuffer*)device())->buffer();
95 const int pref=16;
96 if ( qstrncmp(ch.data(),"QPE/Application/",pref)==0 ) {
97 QString qcopfn("/tmp/qcop-msg-");
98 qcopfn += ch.mid(pref);
99 QFile qcopfile(qcopfn);
100
101 if ( qcopfile.open(IO_WriteOnly | IO_Append) ) {
102 if(flock(qcopfile.handle(), LOCK_EX)) {
103 /* some error occured */
104 qWarning(QString("Failed to obtain file lock on %1 (%2)")
105 .arg(qcopfn).arg( errno ));
106 }
107 /* file locked, but might be stale (e.g. program for whatever
108 reason did not start). I modified more than 1 minute ago,
109 truncate the file */
110 struct stat buf;
111 time_t t;
112 if (!fstat(qcopfile.handle(), &buf) && (time(&t) != (time_t)-1) ) {
113 // success on fstat, lets compare times
114 if (buf.st_mtime + 60 < t) {
115 qWarning("stale file " + qcopfn + " found. Truncating");
116 ftruncate(qcopfile.handle(), 0);
117 qcopfile.reset();
118 }
119 }
120
121 if ( !QCopChannel::isRegistered(ch) ) {
122 int fsize = qcopfile.size();
123 {
124 QDataStream ds(&qcopfile);
125 ds << ch << msg << data;
126 flock(qcopfile.handle(), LOCK_UN);
127 qcopfile.close();
128 }
129
130 if (fsize == 0) {
131 QString cmd = ch.mid(pref);
132 cmd += " -qcop " + qcopfn;
133 Global::invoke(cmd);
134 }
135
136 char c;
137 for (int i=0; (c=msg[i]); i++) {
138 if ( c == ' ' ) {
139 // Return-value required
140 // ###### wait for it
141 break;
142 } else if ( c == '(' ) {
143 // No return value
144 break;
145 }
146 }
147 goto end;
148 } // endif isRegisterd
149 flock(qcopfile.handle(), LOCK_UN);
150 qcopfile.close();
151 qcopfile.remove();
152 } else {
153 qWarning(QString("Failed to obtain file lock on %1")
154 .arg(qcopfn));
155 } // endif open
156 }
157 QCopChannel::send(ch,msg,data);
158end:
159 delete device();
160}
161
162#endif