Diffstat (limited to 'core/tools/quicklauncher/main.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | core/tools/quicklauncher/main.cpp | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/core/tools/quicklauncher/main.cpp b/core/tools/quicklauncher/main.cpp new file mode 100644 index 0000000..e509908 --- a/dev/null +++ b/core/tools/quicklauncher/main.cpp | |||
@@ -0,0 +1,241 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2003 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of the 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 <qpainter.h> | ||
22 | #include <qstrlist.h> | ||
23 | #include <qtimer.h> | ||
24 | #include <qguardedptr.h> | ||
25 | #include <qcopchannel_qws.h> | ||
26 | #define QTOPIA_INTERNAL_INITAPP | ||
27 | #include <qtopia/timezone.h> | ||
28 | #include <qtopia/qpeapplication.h> | ||
29 | #include <qtopia/resource.h> | ||
30 | #include <qtopia/pluginloader.h> | ||
31 | #include <stdio.h> | ||
32 | #include <stdlib.h> | ||
33 | #include <sys/types.h> | ||
34 | #include <sys/stat.h> | ||
35 | #include <unistd.h> | ||
36 | |||
37 | static QPEApplication *app = 0; | ||
38 | static PluginLoader *loader = 0; | ||
39 | static ApplicationInterface *appIface = 0; | ||
40 | static QGuardedPtr<QWidget> mainWindow; | ||
41 | |||
42 | #ifdef _OS_LINUX_ | ||
43 | static char **argv0 = 0; | ||
44 | static int argv_lth; | ||
45 | extern char **environ; | ||
46 | #ifndef SPT_BUFSIZE | ||
47 | #define SPT_BUFSIZE 2048 | ||
48 | #endif | ||
49 | #include <stdarg.h> | ||
50 | void setproctitle (const char *fmt,...) { | ||
51 | int i; | ||
52 | char buf[SPT_BUFSIZE]; | ||
53 | va_list ap; | ||
54 | |||
55 | if (!argv0) | ||
56 | return; | ||
57 | |||
58 | va_start(ap, fmt); | ||
59 | (void) vsnprintf(buf, SPT_BUFSIZE, fmt, ap); | ||
60 | va_end(ap); | ||
61 | |||
62 | i = strlen (buf); | ||
63 | if (i > argv_lth - 2) { | ||
64 | i = argv_lth - 2; | ||
65 | buf[i] = '\0'; | ||
66 | } | ||
67 | memset(argv0[0], '\0', argv_lth); /* clear the memory area */ | ||
68 | (void) strcpy (argv0[0], buf); | ||
69 | |||
70 | argv0[1] = NULL; | ||
71 | } | ||
72 | #endif | ||
73 | |||
74 | |||
75 | class QuickLauncher : public QObject | ||
76 | { | ||
77 | Q_OBJECT | ||
78 | public: | ||
79 | QuickLauncher() : QObject() | ||
80 | { | ||
81 | QCString ch("QPE/QuickLauncher-"); | ||
82 | ch += QString::number(getpid()); | ||
83 | qlChannel = new QCopChannel( ch, this); | ||
84 | connect( qlChannel, SIGNAL(received(const QCString&, const QByteArray&)), | ||
85 | this, SLOT(message(const QCString&, const QByteArray&)) ); | ||
86 | } | ||
87 | |||
88 | static void exec( int /*argc*/, char **argv ) | ||
89 | { | ||
90 | QString appName = argv[0]; | ||
91 | int sep = appName.findRev( '/' ); | ||
92 | if ( sep > 0 ) | ||
93 | appName = appName.mid( sep+1 ); | ||
94 | |||
95 | appIface = 0; | ||
96 | if ( loader->queryInterface(appName, IID_QtopiaApplication, (QUnknownInterface**)&appIface) == QS_OK ) { | ||
97 | mainWindow = appIface->createMainWindow( appName ); | ||
98 | } | ||
99 | if ( mainWindow ) { | ||
100 | if ( mainWindow->metaObject()->slotNames().contains("setDocument(const QString&)") ) { | ||
101 | app->showMainDocumentWidget( mainWindow ); | ||
102 | } else { | ||
103 | app->showMainWidget( mainWindow ); | ||
104 | } | ||
105 | } else { | ||
106 | qWarning( "Could not create application main window" ); | ||
107 | exit(-1); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | private slots: | ||
112 | void message(const QCString &msg, const QByteArray & data) | ||
113 | { | ||
114 | QStrList argList; | ||
115 | |||
116 | if ( msg == "execute(QStrList)" ) { | ||
117 | delete qlChannel; | ||
118 | QDataStream stream( data, IO_ReadOnly ); | ||
119 | QStrList argList; | ||
120 | stream >> argList; | ||
121 | qDebug( "QuickLauncher execute: %s", argList.at(0) ); | ||
122 | doQuickLaunch( argList ); | ||
123 | delete this; | ||
124 | } else if ( msg == "execute(QString)" ) { | ||
125 | delete qlChannel; | ||
126 | QDataStream stream( data, IO_ReadOnly ); | ||
127 | QString arg; | ||
128 | stream >> arg; | ||
129 | qDebug( "QuickLauncher execute: %s", arg.latin1() ); | ||
130 | QStrList argList; | ||
131 | argList.append( arg.utf8() ); | ||
132 | doQuickLaunch( argList ); | ||
133 | delete this; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | private: | ||
138 | void doQuickLaunch( QStrList &argList ) | ||
139 | { | ||
140 | static int myargc = argList.count(); | ||
141 | static char **myargv = new char *[myargc + 1]; | ||
142 | for ( int j = 0; j < myargc; j++ ) { | ||
143 | myargv[j] = new char [strlen(argList.at(j))+1]; | ||
144 | strcpy( myargv[j], argList.at(j) ); | ||
145 | } | ||
146 | myargv[myargc] = NULL; | ||
147 | #ifdef _OS_LINUX_ | ||
148 | // Change name of process | ||
149 | setproctitle(myargv[0]); | ||
150 | #endif | ||
151 | |||
152 | connect(app, SIGNAL(lastWindowClosed()), app, SLOT(hideOrQuit())); | ||
153 | app->exit_loop(); | ||
154 | app->initApp( myargc, myargv ); | ||
155 | exec( myargc, myargv ); | ||
156 | } | ||
157 | |||
158 | private: | ||
159 | QCopChannel *qlChannel; | ||
160 | }; | ||
161 | |||
162 | int main( int argc, char** argv ) | ||
163 | { | ||
164 | app = new QPEApplication( argc, argv ); | ||
165 | |||
166 | loader = new PluginLoader( "application" ); | ||
167 | |||
168 | unsetenv( "LD_BIND_NOW" ); | ||
169 | |||
170 | QCString arg0 = argv[0]; | ||
171 | int sep = arg0.findRev( '/' ); | ||
172 | if ( sep > 0 ) | ||
173 | arg0 = arg0.mid( sep+1 ); | ||
174 | if ( arg0 != "quicklauncher" ) { | ||
175 | qDebug( "QuickLauncher invoked as: %s", arg0.data() ); | ||
176 | QuickLauncher::exec( argc, argv ); | ||
177 | } else { | ||
178 | #ifdef _OS_LINUX_ | ||
179 | // Setup to change proc title | ||
180 | int i; | ||
181 | char **envp = environ; | ||
182 | /* Move the environment so we can reuse the memory. | ||
183 | * (Code borrowed from sendmail.) */ | ||
184 | for (i = 0; envp[i] != NULL; i++) | ||
185 | continue; | ||
186 | environ = (char **) malloc(sizeof(char *) * (i + 1)); | ||
187 | if (environ == NULL) | ||
188 | return -1; | ||
189 | for (i = 0; envp[i] != NULL; i++) | ||
190 | if ((environ[i] = strdup(envp[i])) == NULL) | ||
191 | return -1; | ||
192 | environ[i] = NULL; | ||
193 | |||
194 | argv0 = argv; | ||
195 | if (i > 0) | ||
196 | argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0]; | ||
197 | else | ||
198 | argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0]; | ||
199 | #endif | ||
200 | (void)new QuickLauncher(); | ||
201 | qDebug( "QuickLauncher running" ); | ||
202 | // Pre-load default fonts | ||
203 | QFontMetrics fm( QApplication::font() ); | ||
204 | fm.ascent(); // causes font load. | ||
205 | QFont f( QApplication::font() ); | ||
206 | f.setWeight( QFont::Bold ); | ||
207 | QFontMetrics fmb( f ); | ||
208 | fmb.ascent(); // causes font load. | ||
209 | |||
210 | // Each of the following force internal structures/internal | ||
211 | // initialization to be performed. This may mean allocating | ||
212 | // memory that is not needed by all applications. | ||
213 | TimeZone::current().isValid(); // popuplate timezone cache | ||
214 | TimeString::currentDateFormat(); // create internal structures | ||
215 | TimeString::currentAMPM(); | ||
216 | Resource::loadIconSet("new"); // do internal init | ||
217 | |||
218 | // Create a widget to force initialization of title bar images, etc. | ||
219 | QObject::disconnect(app, SIGNAL(lastWindowClosed()), app, SLOT(hideOrQuit())); | ||
220 | QWidget *w = new QWidget(0,0,Qt::WDestructiveClose|Qt::WStyle_ContextHelp|Qt::WStyle_Tool); | ||
221 | w->setGeometry( -100, -100, 10, 10 ); | ||
222 | w->show(); | ||
223 | QTimer::singleShot( 0, w, SLOT(close()) ); | ||
224 | |||
225 | app->enter_loop(); | ||
226 | } | ||
227 | |||
228 | int rv = app->exec(); | ||
229 | |||
230 | if ( mainWindow ) | ||
231 | delete (QWidget*)mainWindow; | ||
232 | if ( appIface ) | ||
233 | loader->releaseInterface( appIface ); | ||
234 | delete loader; | ||
235 | |||
236 | delete app; | ||
237 | |||
238 | return rv; | ||
239 | } | ||
240 | |||
241 | #include "main.moc" | ||