Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / unx / kde / fpicker / kdecommandthread.cxx
blob6f4411791a96ca112ee61a9a5f6a34e194c0fd19
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 namespace {
78 QStringList* tokenize( const QString &rString )
80 // Commands look like:
81 // command arg1 arg2 arg3 ...
82 // Args may be enclosed in '"', if they contain spaces.
84 QStringList *pList = new QStringList();
86 QString qBuffer;
87 qBuffer.reserve( 1024 );
89 const QChar *pUnicode = rString.unicode();
90 const QChar *pEnd = pUnicode + rString.length();
91 bool bQuoted = false;
93 for ( ; pUnicode != pEnd; ++pUnicode )
95 if ( *pUnicode == '\\' )
97 ++pUnicode;
98 if ( pUnicode != pEnd )
100 if ( *pUnicode == 'n' )
101 qBuffer.append( '\n' );
102 else
103 qBuffer.append( *pUnicode );
106 else if ( *pUnicode == '"' )
107 bQuoted = !bQuoted;
108 else if ( *pUnicode == ' ' && !bQuoted )
110 pList->push_back( qBuffer );
111 qBuffer.setLength( 0 );
113 else
114 qBuffer.append( *pUnicode );
116 pList->push_back( qBuffer );
118 return pList;
123 KDECommandThread::KDECommandThread( QWidget *pObject )
124 : m_pObject( pObject )
128 KDECommandThread::~KDECommandThread()
132 void KDECommandThread::run()
134 QTextIStream qStream( stdin );
135 qStream.setEncoding( QTextStream::UnicodeUTF8 );
137 QString qLine;
138 bool bQuit = false;
139 while ( !bQuit && !qStream.atEnd() )
141 qLine = qStream.readLine();
142 handleCommand( qLine, bQuit );
146 void KDECommandThread::handleCommand( const QString &rString, bool &bQuit )
148 QMutexLocker qMutexLocker( &m_aMutex );
150 #if OSL_DEBUG_LEVEL > 1
151 ::std::cerr << "kdefilepicker received: " << rString.latin1() << ::std::endl;
152 #endif
154 bQuit = false;
155 QStringList *pTokens = tokenize( rString );
157 if ( !pTokens )
158 return;
159 if ( pTokens->empty() )
161 delete pTokens, pTokens = NULL;
162 return;
165 QString qCommand = pTokens->front();
166 pTokens->pop_front();
167 #if OSL_DEBUG_LEVEL > 1
168 ::std::cerr << "kdefilepicker first command: " << qCommand.latin1() << ::std::endl;
169 #endif
171 if ( qCommand == "exit" )
173 bQuit = true;
174 QApplication::exit();
175 kapp->wakeUpGuiThread();
176 #if OSL_DEBUG_LEVEL > 1
177 ::std::cerr << "kdefilepicker: exiting" << ::std::endl;
178 #endif
180 else
181 QApplication::postEvent( m_pObject, new KDECommandEvent( qCommand, pTokens ) );
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */