1 | /**********************************************************************
|
---|
2 | ** Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.
|
---|
3 | **
|
---|
4 | ** This file is part of the Qt Assistant.
|
---|
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 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
12 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
13 | ** Agreement provided with the Software.
|
---|
14 | **
|
---|
15 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
16 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | **
|
---|
18 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
19 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
20 | ** information about Qt Commercial License Agreements.
|
---|
21 | **
|
---|
22 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
23 | ** not clear to you.
|
---|
24 | **
|
---|
25 | **********************************************************************/
|
---|
26 |
|
---|
27 | #include "mainwindow.h"
|
---|
28 | #include "helpdialogimpl.h"
|
---|
29 | #include "config.h"
|
---|
30 |
|
---|
31 | #include <qapplication.h>
|
---|
32 | #include <qserversocket.h>
|
---|
33 | #include <qsocket.h>
|
---|
34 | #include <qpixmap.h>
|
---|
35 | #include <qptrlist.h>
|
---|
36 | #include <qstringlist.h>
|
---|
37 | #include <qdir.h>
|
---|
38 | #include <qmessagebox.h>
|
---|
39 | #include <qguardedptr.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <qtextcodec.h>
|
---|
43 |
|
---|
44 | #if defined(Q_WS_WIN) || defined(Q_WS_PM)
|
---|
45 | #define INDEX_CHECK( text ) if( i+1 >= argc ) { QMessageBox::information( 0, "Qt Assistant", text ); return 1; }
|
---|
46 | #else
|
---|
47 | #define INDEX_CHECK( text ) if( i+1 >= argc ) { fprintf( stderr, text "\n" ); return 1; }
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | static bool allowFirstRun = TRUE;
|
---|
51 |
|
---|
52 | class AssistantSocket : public QSocket
|
---|
53 | {
|
---|
54 | Q_OBJECT
|
---|
55 | public:
|
---|
56 | AssistantSocket( int sock, QObject *parent = 0 );
|
---|
57 | ~AssistantSocket() {}
|
---|
58 |
|
---|
59 | signals:
|
---|
60 | void showLinkRequest( const QString& );
|
---|
61 |
|
---|
62 | private slots:
|
---|
63 | void readClient();
|
---|
64 | void connectionClosed();
|
---|
65 | };
|
---|
66 |
|
---|
67 |
|
---|
68 | class AssistantServer : public QServerSocket
|
---|
69 | {
|
---|
70 | Q_OBJECT
|
---|
71 | public:
|
---|
72 | AssistantServer( QObject* parent = 0 );
|
---|
73 | void newConnection( int socket );
|
---|
74 | Q_UINT16 getPort() const;
|
---|
75 |
|
---|
76 | signals:
|
---|
77 | void showLinkRequest( const QString& );
|
---|
78 | void newConnect();
|
---|
79 |
|
---|
80 | private:
|
---|
81 | Q_UINT16 p;
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | AssistantSocket::AssistantSocket( int sock, QObject *parent )
|
---|
86 | : QSocket( parent, 0 )
|
---|
87 | {
|
---|
88 | connect( this, SIGNAL( readyRead() ),
|
---|
89 | SLOT( readClient() ) );
|
---|
90 | connect( this, SIGNAL( connectionClosed() ),
|
---|
91 | SLOT( connectionClosed() ) );
|
---|
92 | setSocket( sock );
|
---|
93 | }
|
---|
94 |
|
---|
95 | void AssistantSocket::readClient()
|
---|
96 | {
|
---|
97 | QString link = QString::null;
|
---|
98 | while ( canReadLine() )
|
---|
99 | link = readLine();
|
---|
100 | if ( !link.isNull() ) {
|
---|
101 | link = link.replace( "\n", "" );
|
---|
102 | link = link.replace( "\r", "" );
|
---|
103 | emit showLinkRequest( link );
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | void AssistantSocket::connectionClosed()
|
---|
108 | {
|
---|
109 | delete this;
|
---|
110 | }
|
---|
111 |
|
---|
112 | AssistantServer::AssistantServer( QObject *parent )
|
---|
113 | : QServerSocket( 0x7f000001, 0, 1, parent )
|
---|
114 | {
|
---|
115 | if ( !ok() ) {
|
---|
116 | QMessageBox::critical( 0, tr( "Qt Assistant" ),
|
---|
117 | tr( "Failed to bind to port %1" ).arg( port() ) );
|
---|
118 | exit( 1 );
|
---|
119 | }
|
---|
120 | p = port();
|
---|
121 | }
|
---|
122 |
|
---|
123 | Q_UINT16 AssistantServer::getPort() const
|
---|
124 | {
|
---|
125 | return p;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void AssistantServer::newConnection( int socket )
|
---|
129 | {
|
---|
130 | AssistantSocket *as = new AssistantSocket( socket, this );
|
---|
131 | connect( as, SIGNAL( showLinkRequest( const QString& ) ),
|
---|
132 | this, SIGNAL( showLinkRequest( const QString& ) ) );
|
---|
133 | emit newConnect();
|
---|
134 | }
|
---|
135 |
|
---|
136 | int main( int argc, char ** argv )
|
---|
137 | {
|
---|
138 | bool withGUI = TRUE;
|
---|
139 | if ( argc > 1 ) {
|
---|
140 | QString arg( argv[1] );
|
---|
141 | arg = arg.lower();
|
---|
142 | if ( arg == "-addcontentfile"
|
---|
143 | || arg == "-removecontentfile"
|
---|
144 | #if !defined(Q_WS_WIN) && !defined(Q_WS_PM)
|
---|
145 | || arg == "-help"
|
---|
146 | #endif
|
---|
147 | )
|
---|
148 | withGUI = FALSE;
|
---|
149 | }
|
---|
150 | QApplication a(argc, argv, withGUI);
|
---|
151 |
|
---|
152 | QString resourceDir;
|
---|
153 | AssistantServer *as = 0;
|
---|
154 | QStringList catlist;
|
---|
155 | QString file, profileName, aDocPath;
|
---|
156 | bool server = FALSE;
|
---|
157 | bool hideSidebar = FALSE;
|
---|
158 | bool startClean = FALSE;
|
---|
159 | bool configLoaded = FALSE;
|
---|
160 | if ( argc == 2 ) {
|
---|
161 | if ( (argv[1])[0] != '-' )
|
---|
162 | file = argv[1];
|
---|
163 | }
|
---|
164 | if ( file.isEmpty() ) {
|
---|
165 | for ( int i = 1; i < argc; i++ ) {
|
---|
166 | if ( QString( argv[i] ).lower() == "-file" ) {
|
---|
167 | INDEX_CHECK( "Missing file argument!" );
|
---|
168 | i++;
|
---|
169 | file = argv[i];
|
---|
170 | } else if ( QString( argv[i] ).lower() == "-server" ) {
|
---|
171 | server = TRUE;
|
---|
172 | } else if ( QString( argv[i] ).lower() == "-profile" ) {
|
---|
173 | INDEX_CHECK( "Missing profile argument!" );
|
---|
174 | profileName = argv[++i];
|
---|
175 | } else if ( QString( argv[i] ).lower() == "-addcontentfile" ) {
|
---|
176 | INDEX_CHECK( "Missing content file!" );
|
---|
177 | Config *c = Config::loadConfig( QString::null );
|
---|
178 | QFileInfo file( argv[i+1] );
|
---|
179 | if( !file.exists() ) {
|
---|
180 | fprintf( stderr, "Could not locate content file: '%s'\n",
|
---|
181 | file.absFilePath().latin1() );
|
---|
182 | fflush( stderr );
|
---|
183 | return 1;
|
---|
184 | }
|
---|
185 | DocuParser *parser = DocuParser::createParser( file.absFilePath() );
|
---|
186 | if( parser ) {
|
---|
187 | QFile f( argv[i+1] );
|
---|
188 | if( !parser->parse( &f ) ) {
|
---|
189 | fprintf( stderr, "Failed to parse file: '%s'\n, ",
|
---|
190 | file.absFilePath().latin1() );
|
---|
191 | fflush( stderr );
|
---|
192 | return 1;
|
---|
193 | }
|
---|
194 | parser->addTo( c->profile() );
|
---|
195 | c->setDocRebuild( TRUE );
|
---|
196 | c->save();
|
---|
197 | }
|
---|
198 | return 0;
|
---|
199 | } else if ( QString( argv[i] ).lower() == "-removecontentfile" ) {
|
---|
200 | INDEX_CHECK( "Missing content file!" );
|
---|
201 | Config *c = Config::loadConfig( QString::null );
|
---|
202 | Profile *profile = c->profile();
|
---|
203 | QStringList entries = profile->docs.grep(argv[i+1]);
|
---|
204 | if (entries.count() == 0) {
|
---|
205 | fprintf(stderr, "Could not locate content file: '%s'\n",
|
---|
206 | argv[i+1]);
|
---|
207 | fflush(stderr);
|
---|
208 | return 1;
|
---|
209 | } else if (entries.count() > 1) {
|
---|
210 | fprintf(stderr, "More than one entry matching file name found, "
|
---|
211 | "please specify full path to file");
|
---|
212 | fflush(stderr);
|
---|
213 | return 1;
|
---|
214 | } else {
|
---|
215 | QFileInfo file(entries[0]);
|
---|
216 | if( !file.exists() ) {
|
---|
217 | fprintf( stderr, "Could not locate content file: '%s'\n",
|
---|
218 | file.absFilePath().latin1() );
|
---|
219 | fflush( stderr );
|
---|
220 | return 1;
|
---|
221 | }
|
---|
222 | profile->removeDocFileEntry( file.absFilePath() );
|
---|
223 | c->setDocRebuild( TRUE );
|
---|
224 | c->save();
|
---|
225 | }
|
---|
226 | return 0;
|
---|
227 | } else if ( QString( argv[i] ).lower() == "-docpath" ) {
|
---|
228 | INDEX_CHECK( "Missing path!" );
|
---|
229 | QDir dir( argv[i+1] );
|
---|
230 | if ( dir.exists() ) {
|
---|
231 | Config *c = Config::loadConfig( QString::null );
|
---|
232 | c->saveProfile(Profile::createDefaultProfile(dir.absPath()));
|
---|
233 | c->loadDefaultProfile();
|
---|
234 | c->setDocRebuild( TRUE );
|
---|
235 | c->save();
|
---|
236 | configLoaded = TRUE;
|
---|
237 | ++i;
|
---|
238 | } else {
|
---|
239 | fprintf( stderr, "The specified path does not exist!\n");
|
---|
240 | fflush( stderr );
|
---|
241 | return 1;
|
---|
242 | }
|
---|
243 | } else if ( QString( argv[i] ).lower() == "-hidesidebar" ) {
|
---|
244 | hideSidebar = TRUE;
|
---|
245 | } else if ( QString( argv[i] ).lower() == "-help" ) {
|
---|
246 | QString helpText( "Usage: assistant [option]\n"
|
---|
247 | "Options:\n"
|
---|
248 | " -file Filename assistant opens the specified file\n"
|
---|
249 | " -server reads commands from a socket after\n"
|
---|
250 | " assistant has started\n"
|
---|
251 | " -profile fileName starts assistant and displays the\n"
|
---|
252 | " profile specified in the file fileName.\n"
|
---|
253 | " -addContentFile file adds the content file 'file' to the set of\n"
|
---|
254 | " documentation available by default\n"
|
---|
255 | " -removeContentFile file removes the content file 'file' from the\n"
|
---|
256 | " documentation available by default\n"
|
---|
257 | " -docPath path sets the Qt documentation root path to\n"
|
---|
258 | " 'path' and starts assistant\n"
|
---|
259 | " -hideSidebar assistant will hide the sidebar.\n"
|
---|
260 | " -resourceDir assistant will load translations from\n"
|
---|
261 | " this directory.\n"
|
---|
262 | " -help shows this help.");
|
---|
263 | #if defined(Q_WS_WIN) || defined(Q_WS_PM)
|
---|
264 | QMessageBox::information( 0, "Qt Assistant", "<pre>" + helpText + "</pre>" );
|
---|
265 | #else
|
---|
266 | printf( "%s\n", helpText.latin1() );
|
---|
267 | #endif
|
---|
268 | exit( 0 );
|
---|
269 | } else if ( QString( argv[i] ).lower() == "-resourcedir" ) {
|
---|
270 | INDEX_CHECK( "Missing resource directory argument!" );
|
---|
271 | resourceDir = QString( argv[++i] );
|
---|
272 | } else {
|
---|
273 | fprintf( stderr, "Unrecognized option '%s'. Try -help to get help.\n",
|
---|
274 | argv[i] );
|
---|
275 | fflush( stderr );
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | if( resourceDir.isNull() )
|
---|
282 | resourceDir = qInstallPathTranslations();
|
---|
283 |
|
---|
284 | QTranslator translator( 0 );
|
---|
285 | translator.load( QString("assistant_") + QTextCodec::locale(), resourceDir );
|
---|
286 | a.installTranslator( &translator );
|
---|
287 |
|
---|
288 | QTranslator qtTranslator( 0 );
|
---|
289 | qtTranslator.load( QString("qt_") + QTextCodec::locale(), resourceDir );
|
---|
290 | a.installTranslator( &qtTranslator );
|
---|
291 |
|
---|
292 |
|
---|
293 | Config *conf = 0;
|
---|
294 | if (configLoaded)
|
---|
295 | conf = Config::configuration();
|
---|
296 | else
|
---|
297 | conf = Config::loadConfig( profileName );
|
---|
298 | if (!conf) {
|
---|
299 | fprintf( stderr, "Profile '%s' does not exist!\n", profileName.latin1() );
|
---|
300 | fflush( stderr );
|
---|
301 | return -1;
|
---|
302 | }
|
---|
303 |
|
---|
304 | bool max = conf->isMaximized();
|
---|
305 | QStringList links = conf->source();
|
---|
306 | conf->hideSideBar( hideSidebar );
|
---|
307 |
|
---|
308 | QGuardedPtr<MainWindow> mw = new MainWindow( 0, "Assistant" );
|
---|
309 |
|
---|
310 | if ( server ) {
|
---|
311 | as = new AssistantServer();
|
---|
312 | printf("%d\n", as->port() );
|
---|
313 | fflush( stdout );
|
---|
314 | as->connect( as, SIGNAL( showLinkRequest( const QString& ) ),
|
---|
315 | mw, SLOT( showLinkFromClient( const QString& ) ) );
|
---|
316 | }
|
---|
317 |
|
---|
318 | if ( max )
|
---|
319 | mw->showMaximized();
|
---|
320 | else
|
---|
321 | mw->show();
|
---|
322 |
|
---|
323 | if ( !file.isEmpty() )
|
---|
324 | mw->showLink( file );
|
---|
325 | else if ( file.isEmpty() )
|
---|
326 | mw->showLinks( links );
|
---|
327 |
|
---|
328 | a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
|
---|
329 |
|
---|
330 | int appExec = a.exec();
|
---|
331 | delete (MainWindow*)mw;
|
---|
332 | return appExec;
|
---|
333 | }
|
---|
334 |
|
---|
335 | #include "main.moc"
|
---|