Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / vcl / unx / kde / fpicker / kdecommandthread.cxx
blob55559d2117f1b8c994804816ff3d6aa3317296c9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cstddef>
22 #include <kdecommandthread.hxx>
24 #include <config_vclplug.h>
26 #if ENABLE_TDE
27 #include <tqstringlist.h>
28 #include <tdeapplication.h>
29 #else // ENABLE_TDE
30 #include <qstringlist.h>
31 #include <kapplication.h>
32 #endif // ENABLE_TDE
34 #if OSL_DEBUG_LEVEL > 1
35 #include <iostream>
36 #endif
38 //////////////////////////////////////////////////////////////////////////
39 // CommandEvent
40 //////////////////////////////////////////////////////////////////////////
42 KDECommandEvent::KDECommandEvent( const QString &qCommand, QStringList *pStringList )
43 : QCustomEvent( TypeId, pStringList ),
44 m_eCommand( Unknown )
46 struct {
47 const char *pName;
48 CommandEventType eType;
49 } *pIdx, pMapping[] =
51 { "appendControl", AppendControl },
52 { "enableControl", EnableControl },
53 { "getValue", GetValue },
54 { "setValue", SetValue },
55 { "appendFilter", AppendFilter },
56 { "appendFilterGroup", AppendFilterGroup },
57 { "getCurrentFilter", GetCurrentFilter },
58 { "setCurrentFilter", SetCurrentFilter },
59 { "getDirectory", GetDirectory },
60 { "setDirectory", SetDirectory },
61 { "getFiles", GetFiles },
62 { "setTitle", SetTitle },
63 { "setType", SetType },
64 { "setDefaultName", SetDefaultName },
65 { "setMultiSelection", SetMultiSelection },
66 { "exec", Exec },
67 { 0, Unknown }
70 for ( pIdx = pMapping; pIdx->pName && qCommand != pIdx->pName; ++pIdx )
73 m_eCommand = pIdx->eType;
76 //////////////////////////////////////////////////////////////////////////
77 // CommandThread
78 //////////////////////////////////////////////////////////////////////////
80 KDECommandThread::KDECommandThread( QWidget *pObject )
81 : m_pObject( pObject )
85 KDECommandThread::~KDECommandThread()
89 void KDECommandThread::run()
91 QTextIStream qStream( stdin );
92 qStream.setEncoding( QTextStream::UnicodeUTF8 );
94 QString qLine;
95 bool bQuit = false;
96 while ( !bQuit && !qStream.atEnd() )
98 qLine = qStream.readLine();
99 handleCommand( qLine, bQuit );
103 void KDECommandThread::handleCommand( const QString &rString, bool &bQuit )
105 QMutexLocker qMutexLocker( &m_aMutex );
107 #if OSL_DEBUG_LEVEL > 1
108 ::std::cerr << "kdefilepicker received: " << rString.latin1() << ::std::endl;
109 #endif
111 bQuit = false;
112 QStringList *pTokens = tokenize( rString );
114 if ( !pTokens )
115 return;
116 if ( pTokens->empty() )
118 delete pTokens, pTokens = NULL;
119 return;
122 QString qCommand = pTokens->front();
123 pTokens->pop_front();
124 #if OSL_DEBUG_LEVEL > 1
125 ::std::cerr << "kdefilepicker first command: " << qCommand.latin1() << ::std::endl;
126 #endif
128 if ( qCommand == "exit" )
130 bQuit = true;
131 kapp->exit();
132 kapp->wakeUpGuiThread();
133 #if OSL_DEBUG_LEVEL > 1
134 ::std::cerr << "kdefilepicker: exiting" << ::std::endl;
135 #endif
137 else
138 kapp->postEvent( m_pObject, new KDECommandEvent( qCommand, pTokens ) );
141 QStringList* KDECommandThread::tokenize( const QString &rString )
143 // Commands look like:
144 // command arg1 arg2 arg3 ...
145 // Args may be enclosed in '"', if they contain spaces.
147 QStringList *pList = new QStringList();
149 QString qBuffer;
150 qBuffer.reserve( 1024 );
152 const QChar *pUnicode = rString.unicode();
153 const QChar *pEnd = pUnicode + rString.length();
154 bool bQuoted = false;
156 for ( ; pUnicode != pEnd; ++pUnicode )
158 if ( *pUnicode == '\\' )
160 ++pUnicode;
161 if ( pUnicode != pEnd )
163 if ( *pUnicode == 'n' )
164 qBuffer.append( '\n' );
165 else
166 qBuffer.append( *pUnicode );
169 else if ( *pUnicode == '"' )
170 bQuoted = !bQuoted;
171 else if ( *pUnicode == ' ' && !bQuoted )
173 pList->push_back( qBuffer );
174 qBuffer.setLength( 0 );
176 else
177 qBuffer.append( *pUnicode );
179 pList->push_back( qBuffer );
181 return pList;
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */