Update ooo320-m1
[ooovba.git] / fpicker / source / unx / kde / kdecommandthread.cxx
blob56c3c38e41919d2330b3e6df044d5af2603eef83
1 /*************************************************************************
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 * MA 02111-1307 USA
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Sun Microsystems, Inc.
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Jan Holesovsky <kendy@openoffice.org>
60 ************************************************************************/
62 #include <kdecommandthread.hxx>
64 #include <qstringlist.h>
66 #include <kapplication.h>
68 #include <iostream>
70 //////////////////////////////////////////////////////////////////////////
71 // CommandEvent
72 //////////////////////////////////////////////////////////////////////////
74 CommandEvent::CommandEvent( const QString &qCommand, QStringList *pStringList )
75 : QCustomEvent( TypeId, pStringList ),
76 m_eCommand( Unknown )
78 struct {
79 const char *pName;
80 CommandEventType eType;
81 } *pIdx, pMapping[] =
83 { "appendControl", AppendControl },
84 { "enableControl", EnableControl },
85 { "getValue", GetValue },
86 { "setValue", SetValue },
87 { "appendFilter", AppendFilter },
88 { "appendFilterGroup", AppendFilterGroup },
89 { "getCurrentFilter", GetCurrentFilter },
90 { "setCurrentFilter", SetCurrentFilter },
91 { "getDirectory", GetDirectory },
92 { "setDirectory", SetDirectory },
93 { "getFiles", GetFiles },
94 { "setTitle", SetTitle },
95 { "setType", SetType },
96 { "setDefaultName", SetDefaultName },
97 { "setMultiSelection", SetMultiSelection },
98 { "exec", Exec },
99 { 0, Unknown }
102 for ( pIdx = pMapping; pIdx->pName && qCommand != pIdx->pName; ++pIdx )
105 m_eCommand = pIdx->eType;
108 //////////////////////////////////////////////////////////////////////////
109 // CommandThread
110 //////////////////////////////////////////////////////////////////////////
112 CommandThread::CommandThread( QWidget *pObject )
113 : m_pObject( pObject )
117 CommandThread::~CommandThread()
121 void CommandThread::run()
123 QTextIStream qStream( stdin );
124 qStream.setEncoding( QTextStream::UnicodeUTF8 );
126 QString qLine;
127 bool bQuit = false;
128 while ( !bQuit && !qStream.atEnd() )
130 qLine = qStream.readLine();
131 handleCommand( qLine, bQuit );
135 void CommandThread::handleCommand( const QString &rString, bool &bQuit )
137 QMutexLocker qMutexLocker( &m_aMutex );
139 #if OSL_DEBUG_LEVEL > 0
140 ::std::cerr << "kdefilepicker received: " << rString.latin1() << ::std::endl;
141 #endif
143 bQuit = false;
144 QStringList *pTokens = tokenize( rString );
146 if ( !pTokens )
147 return;
148 if ( pTokens->empty() )
150 delete pTokens, pTokens = NULL;
151 return;
154 QString qCommand = pTokens->front();
155 pTokens->pop_front();
157 if ( qCommand == "exit" )
159 bQuit = true;
160 kapp->exit();
161 kapp->wakeUpGuiThread();
163 else
164 kapp->postEvent( m_pObject, new CommandEvent( qCommand, pTokens ) );
167 QStringList* CommandThread::tokenize( const QString &rString )
169 // Commands look like:
170 // command arg1 arg2 arg3 ...
171 // Args may be enclosed in '"', if they contain spaces.
173 QStringList *pList = new QStringList();
175 QString qBuffer;
176 qBuffer.reserve( 1024 );
178 const QChar *pUnicode = rString.unicode();
179 const QChar *pEnd = pUnicode + rString.length();
180 bool bQuoted = false;
182 for ( ; pUnicode != pEnd; ++pUnicode )
184 if ( *pUnicode == '\\' )
186 ++pUnicode;
187 if ( pUnicode != pEnd )
189 if ( *pUnicode == 'n' )
190 qBuffer.append( '\n' );
191 else
192 qBuffer.append( *pUnicode );
195 else if ( *pUnicode == '"' )
196 bQuoted = !bQuoted;
197 else if ( *pUnicode == ' ' && !bQuoted )
199 pList->push_back( qBuffer );
200 qBuffer.setLength( 0 );
202 else
203 qBuffer.append( *pUnicode );
205 pList->push_back( qBuffer );
207 return pList;