1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <kdecommandthread.hxx>
24 #include <config_vclplug.h>
27 #include <tqstringlist.h>
28 #include <tdeapplication.h>
30 #include <qstringlist.h>
31 #include <kapplication.h>
34 #if OSL_DEBUG_LEVEL > 1
40 KDECommandEvent::KDECommandEvent( const QString
&qCommand
, QStringList
*pStringList
)
41 : QCustomEvent( TypeId
, pStringList
),
46 CommandEventType eType
;
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
},
68 for ( pIdx
= pMapping
; pIdx
->pName
&& qCommand
!= pIdx
->pName
; ++pIdx
)
71 m_eCommand
= pIdx
->eType
;
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
);
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
;
108 QStringList
*pTokens
= tokenize( rString
);
112 if ( pTokens
->empty() )
114 delete pTokens
, pTokens
= NULL
;
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
;
124 if ( qCommand
== "exit" )
128 kapp
->wakeUpGuiThread();
129 #if OSL_DEBUG_LEVEL > 1
130 ::std::cerr
<< "kdefilepicker: exiting" << ::std::endl
;
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();
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
== '\\' )
157 if ( pUnicode
!= pEnd
)
159 if ( *pUnicode
== 'n' )
160 qBuffer
.append( '\n' );
162 qBuffer
.append( *pUnicode
);
165 else if ( *pUnicode
== '"' )
167 else if ( *pUnicode
== ' ' && !bQuoted
)
169 pList
->push_back( qBuffer
);
170 qBuffer
.setLength( 0 );
173 qBuffer
.append( *pUnicode
);
175 pList
->push_back( qBuffer
);
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */