summaryrefslogtreecommitdiff
path: root/library/qcopenvelope_qws.cpp
blob: 10d1567dff27f7c1985800f7acfc14b06952b7b9 (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qcopenvelope_qws.h"
#include "global.h"
#include <qbuffer.h>
#include <qdatastream.h>
#include <qfile.h>
#include <unistd.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

#ifndef QT_NO_COP

/*!
  \class QCopEnvelope qcopenvelope_qws.h
  \brief The QCopEnvelope class encapsulates QCop message sending.

  QCop messages allow applications to communicate with each other.
  These messages are send using QCopEnvelope, and received by connecting
  to a QCopChannel.

  To send a message, use the following protocol:

  \code
     QCopEnvelope e(channelname, messagename);
     e << parameter1 << parameter2 << ...;
  \endcode

  For messages without parameters, you can simply use:

  \code
     QCopEnvelope (channelname, messagename);
  \endcode

  Do not try to simplify further as some compilers may not do
  as you expect.

  The <tt>channelname</tt> of channels within Qtopia all start with "QPE/".
  The <tt>messagename</tt> is a function identifier followed by a list of types
  in parentheses. There are no spaces in the message name.

  To receive a message, you will generally just use your applications
  predefined QPE/Application/<i>appname</i> channel
  (see QPEApplication::appMessage()), but you can make another channel
  and connect it to a slot with:

  \code
      myChannel = new QCopChannel( "QPE/FooBar", this );
      connect( myChannel, SIGNAL(received(const QCString &, const QByteArray &)),
               this, SLOT(fooBarMessage( const QCString &, const QByteArray &)) );
  \endcode

  See also, the \link qcop.html list of Qtopia messages\endlink.
*/

/*!
  Constructs a QCopEnvelope that will write \a message to \a channel.
  If \a message has parameters, you must then user operator<<() to
  write the parameters.
*/
QCopEnvelope::QCopEnvelope( const QCString& channel, const QCString& message ) :
    QDataStream(new QBuffer),
    ch(channel), msg(message)
{
    device()->open(IO_WriteOnly);
}

/*!
  Writes the completed message and destroys the QCopEnvelope.
*/
QCopEnvelope::~QCopEnvelope()
{
    QByteArray data = ((QBuffer*)device())->buffer();
    const int pref=16;
    if ( qstrncmp(ch.data(),"QPE/Application/",pref)==0 ) {
	QString qcopfn("/tmp/qcop-msg-");
	qcopfn += ch.mid(pref);
	QFile qcopfile(qcopfn);

	if ( qcopfile.open(IO_WriteOnly | IO_Append) ) {
	    if(flock(qcopfile.handle(), LOCK_EX)) {
		/* some error occured */
		qWarning(QString("Failed to obtain file lock on %1 (%2)")
			.arg(qcopfn).arg( errno ));
	    }
	    /* file locked, but might be stale (e.g. program for whatever
	       reason did not start).  I modified more than 1 minute ago,
	       truncate the file */
	    struct stat buf;
	    time_t t;
	    if (!fstat(qcopfile.handle(), &buf) &&  (time(&t) != (time_t)-1) ) {
		// success on fstat, lets compare times
		if (buf.st_mtime + 60 < t) {
		    qWarning("stale file " + qcopfn + " found.  Truncating");
		    ftruncate(qcopfile.handle(), 0);
		    qcopfile.reset();
		}
	    }

	    if ( !QCopChannel::isRegistered(ch) ) {
		int fsize = qcopfile.size();
		{
		    QDataStream ds(&qcopfile);
		    ds << ch << msg << data;
		    flock(qcopfile.handle(), LOCK_UN);
		    qcopfile.close();
		}

		if (fsize == 0) {
		    QString cmd = ch.mid(pref);
		    cmd += " -qcop " + qcopfn;
		    Global::invoke(cmd);
		}

		char c;
		for (int i=0; (c=msg[i]); i++) {
		    if ( c == ' ' ) {
			// Return-value required
			// ###### wait for it
			break;
		    } else if ( c == '(' ) {
			// No return value
			break;
		    }
		}
		goto end;
	    } // endif isRegisterd
	    flock(qcopfile.handle(), LOCK_UN);
	    qcopfile.close();
	    qcopfile.remove();
	} else {
	    qWarning(QString("Failed to obtain file lock on %1")
			.arg(qcopfn));
	} // endif open
    }
    QCopChannel::send(ch,msg,data);
end:
    delete device();
}

#endif