Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / unx / kde / fpicker / kdecommandthread.cxx
blob533950abd9f71a7cba855ae2c4aca1103baef07a
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 // CommandEvent
40 KDECommandEvent::KDECommandEvent( const QString &qCommand, QStringList *pStringList )
41 : QCustomEvent( TypeId, pStringList ),
42 m_eCommand( Unknown )
44 struct {
45 const char *pName;
46 CommandEventType eType;
47 } *pIdx, pMapping[] =
49 { "appendControl", AppendControl },
50 { "enableControl", EnableControl },
51 { "getValue", GetValue },
52 { "setValue", SetValue },
53 { "appendFilter", AppendFilter },
54 { "appendFilterGroup", AppendFilterGroup },
55 { "getCurrentFilter", GetCurrentFilter },
56 { "setCurrentFilter", SetCurrentFilter },
57 { "getDirectory", GetDirectory },
58 { "setDirectory", SetDirectory },
59 { "getFiles", GetFiles },
60 { "setTitle", SetTitle },
61 { "setType", SetType },
62 { "setDefaultName", SetDefaultName },
63 { "setMultiSelection", SetMultiSelection },
64 { "exec", Exec },
65 { 0, Unknown }
68 for ( pIdx = pMapping; pIdx->pName && qCommand != pIdx->pName; ++pIdx )
71 m_eCommand = pIdx->eType;
74 // CommandThread
76 KDECommandThread::KDECommandThread( QWidget *pObject )
77 : m_pObject( pObject )
81 KDECommandThread::~KDECommandThread()
85 void KDECommandThread::run()
87 QTextIStream qStream( stdin );
88 qStream.setEncoding( QTextStream::UnicodeUTF8 );
90 QString qLine;
91 bool bQuit = false;
92 while ( !bQuit && !qStream.atEnd() )
94 qLine = qStream.readLine();
95 handleCommand( qLine, bQuit );
99 void KDECommandThread::handleCommand( const QString &rString, bool &bQuit )
101 QMutexLocker qMutexLocker( &m_aMutex );
103 #if OSL_DEBUG_LEVEL > 1
104 ::std::cerr << "kdefilepicker received: " << rString.latin1() << ::std::endl;
105 #endif
107 bQuit = false;
108 QStringList *pTokens = tokenize( rString );
110 if ( !pTokens )
111 return;
112 if ( pTokens->empty() )
114 delete pTokens, pTokens = NULL;
115 return;
118 QString qCommand = pTokens->front();
119 pTokens->pop_front();
120 #if OSL_DEBUG_LEVEL > 1
121 ::std::cerr << "kdefilepicker first command: " << qCommand.latin1() << ::std::endl;
122 #endif
124 if ( qCommand == "exit" )
126 bQuit = true;
127 kapp->exit();
128 kapp->wakeUpGuiThread();
129 #if OSL_DEBUG_LEVEL > 1
130 ::std::cerr << "kdefilepicker: exiting" << ::std::endl;
131 #endif
133 else
134 kapp->postEvent( m_pObject, new KDECommandEvent( qCommand, pTokens ) );
137 QStringList* KDECommandThread::tokenize( const QString &rString )
139 // Commands look like:
140 // command arg1 arg2 arg3 ...
141 // Args may be enclosed in '"', if they contain spaces.
143 QStringList *pList = new QStringList();
145 QString qBuffer;
146 qBuffer.reserve( 1024 );
148 const QChar *pUnicode = rString.unicode();
149 const QChar *pEnd = pUnicode + rString.length();
150 bool bQuoted = false;
152 for ( ; pUnicode != pEnd; ++pUnicode )
154 if ( *pUnicode == '\\' )
156 ++pUnicode;
157 if ( pUnicode != pEnd )
159 if ( *pUnicode == 'n' )
160 qBuffer.append( '\n' );
161 else
162 qBuffer.append( *pUnicode );
165 else if ( *pUnicode == '"' )
166 bQuoted = !bQuoted;
167 else if ( *pUnicode == ' ' && !bQuoted )
169 pList->push_back( qBuffer );
170 qBuffer.setLength( 0 );
172 else
173 qBuffer.append( *pUnicode );
175 pList->push_back( qBuffer );
177 return pList;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */