Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / vcl / unx / kde / UnxCommandThread.cxx
blob7b907dea9c66a445844f9dc3b0d0b8a332510f87
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 <UnxCommandThread.hxx>
21 #include <UnxNotifyThread.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
26 #include <unistd.h>
27 #include <string.h>
28 #include <iostream>
30 using namespace ::com::sun::star;
32 // UnxFilePickerCommandThread
34 UnxFilePickerCommandThread::UnxFilePickerCommandThread( UnxFilePickerNotifyThread *pNotifyThread, int nReadFD )
35 : m_pNotifyThread( pNotifyThread ),
36 m_nReadFD( nReadFD )
40 UnxFilePickerCommandThread::~UnxFilePickerCommandThread()
44 bool SAL_CALL UnxFilePickerCommandThread::result()
46 ::osl::MutexGuard aGuard( m_aMutex );
48 return m_aResult;
51 OUString SAL_CALL UnxFilePickerCommandThread::getCurrentFilter()
53 ::osl::MutexGuard aGuard( m_aMutex );
55 return m_aGetCurrentFilter;
58 OUString SAL_CALL UnxFilePickerCommandThread::getDirectory()
60 ::osl::MutexGuard aGuard( m_aMutex );
62 return m_aGetDirectory;
65 uno::Sequence< OUString > SAL_CALL UnxFilePickerCommandThread::getFiles()
67 ::osl::MutexGuard aGuard( m_aMutex );
69 sal_Int32 nSize = m_aGetFiles.size();
70 uno::Sequence< OUString > aFiles( ( nSize > 1 )? nSize + 1: nSize );
72 if ( nSize == 1 )
73 aFiles[0] = m_aGetFiles.front();
74 else if ( nSize > 1 )
76 // First entry in the sequence must be the dirname, the others are the
77 // filenames, so we have to rearrange the list...
79 OUString aFront = m_aGetFiles.front();
80 sal_Int32 nLastSlash = aFront.lastIndexOf( '/' );
82 aFiles[0] = ( nLastSlash >= 0 )? aFront.copy( 0, nLastSlash ): OUString();
83 ++nLastSlash;
85 sal_Int32 nIdx = 1;
86 for ( ::std::list< OUString >::const_iterator it = m_aGetFiles.begin();
87 it != m_aGetFiles.end(); ++it, ++nIdx )
89 sal_Int32 nLength = (*it).getLength() - nLastSlash;
90 aFiles[nIdx] = ( nLength >= 0 )? (*it).copy( nLastSlash, nLength ): OUString();
94 return aFiles;
97 uno::Any SAL_CALL UnxFilePickerCommandThread::getValue()
99 ::osl::MutexGuard aGuard( m_aMutex );
101 return m_aGetValue;
104 void SAL_CALL UnxFilePickerCommandThread::run()
106 if ( m_nReadFD < 0 )
107 return;
109 sal_Int32 nBufferSize = 1024; // 1 is for testing, 1024 for real use
110 sal_Char *pBuffer = new sal_Char[nBufferSize];
111 sal_Char *pBufferEnd = pBuffer + nBufferSize;
113 sal_Char *pWhereToRead = pBuffer;
114 sal_Char *pEntryBegin = pBuffer;
115 sal_Int32 nBytesRead = 0;
116 bool bShouldExit = false;
117 while ( !bShouldExit && ( nBytesRead = read( m_nReadFD, pWhereToRead, pBufferEnd - pWhereToRead ) ) > 0 )
119 bool bFoundNL = false;
120 sal_Char *pWhereToReadEnd = pWhereToRead + nBytesRead;
121 sal_Char *pEntryEnd = pWhereToRead;
122 do {
123 for ( ; pEntryEnd < pWhereToReadEnd && *pEntryEnd != '\n'; ++pEntryEnd )
126 if ( pEntryEnd < pWhereToReadEnd )
128 bFoundNL = true;
129 *pEntryEnd = 0;
131 if ( strcmp( pEntryBegin, "exited" ) == 0 )
132 bShouldExit = true;
133 else
134 handleCommand( OUString( pEntryBegin, pEntryEnd - pEntryBegin, RTL_TEXTENCODING_UTF8 )/*, bQuit*/ );
136 pEntryBegin = pEntryEnd + 1;
138 } while ( pEntryEnd < pWhereToReadEnd );
140 if ( bFoundNL )
142 if ( pEntryBegin < pBufferEnd )
143 memmove( pBuffer, pEntryBegin, pWhereToReadEnd - pEntryBegin );
145 else
147 // enlarge the buffer size
148 nBufferSize *= 2;
149 sal_Char *pNewBuffer = new sal_Char[nBufferSize];
150 if ( pEntryBegin < pBufferEnd )
151 memcpy( pNewBuffer, pEntryBegin, pWhereToReadEnd - pEntryBegin );
153 delete[] pBuffer;
154 pBuffer = pNewBuffer;
155 pBufferEnd = pBuffer + nBufferSize;
158 pWhereToRead = pBuffer + ( pWhereToReadEnd - pEntryBegin );
159 pEntryBegin = pBuffer;
163 void SAL_CALL UnxFilePickerCommandThread::handleCommand( const OUString &rCommand )
165 ::osl::MutexGuard aGuard( m_aMutex );
167 #if OSL_DEBUG_LEVEL > 0
168 ::std::cerr << "UnxFilePicker received: \"" <<
169 OUStringToOString( rCommand, RTL_TEXTENCODING_ASCII_US ).getStr() << "\"" << ::std::endl;
170 #endif
172 ::std::list< OUString > aList = tokenize( rCommand );
174 if ( aList.empty() )
175 return;
177 OUString aCommandName = aList.front();
178 aList.pop_front();
180 if ( aCommandName == "accept" )
182 m_aResult = true;
183 m_aExecCondition.set();
185 else if ( aCommandName == "reject" )
187 m_aResult = false;
188 m_aExecCondition.set();
190 else if ( aCommandName == "fileSelectionChanged" )
192 if ( m_pNotifyThread )
193 m_pNotifyThread->fileSelectionChanged();
195 else if ( aCommandName == "files" )
197 m_aGetFiles = aList;
198 m_aGetFilesCondition.set();
200 else if ( aCommandName == "value" )
202 OUString aType;
203 if ( !aList.empty() )
205 aType = aList.front();
206 aList.pop_front();
209 if ( aType == "bool" )
211 bool bValue = !aList.empty() && aList.front().equalsIgnoreAsciiCase("true");
213 m_aGetValue <<= bValue;
214 m_aGetValueCondition.set();
216 else if ( aType == "int" )
218 sal_Int32 nValue = 0;
219 if ( !aList.empty() )
220 nValue = aList.front().toInt32();
222 m_aGetValue <<= nValue;
223 m_aGetValueCondition.set();
225 else if ( aType == "string" )
227 OUString aValue;
228 if ( !aList.empty() )
229 aValue = aList.front();
231 m_aGetValue <<= aValue;
232 m_aGetValueCondition.set();
234 else if ( aType == "stringList" )
236 uno::Sequence< OUString > aSequence( aList.size() );
237 sal_Int32 nIdx = 0;
238 for ( ::std::list< OUString >::const_iterator it = aList.begin(); it != aList.end(); ++it, ++nIdx )
239 aSequence[nIdx] = (*it);
241 m_aGetValue <<= aSequence;
242 m_aGetValueCondition.set();
244 else
246 m_aGetValue = uno::Any();
247 m_aGetValueCondition.set();
250 else if ( aCommandName == "currentFilter" )
252 m_aGetCurrentFilter = aList.empty()? OUString(): aList.front();
253 m_aGetCurrentFilterCondition.set();
255 else if ( aCommandName == "currentDirectory" )
257 m_aGetDirectory = aList.empty()? OUString(): aList.front();
258 m_aGetDirectoryCondition.set();
260 else
262 #if OSL_DEBUG_LEVEL > 0
263 ::std::cerr << "Unrecognized command: "
264 << OUStringToOString( aCommandName, RTL_TEXTENCODING_ASCII_US ).getStr() << "\"" << ::std::endl;
265 #endif
269 ::std::list< OUString > SAL_CALL UnxFilePickerCommandThread::tokenize( const OUString &rCommand )
271 ::std::list< OUString > aList;
272 OUStringBuffer aBuffer( 1024 );
274 const sal_Unicode *pUnicode = rCommand.getStr();
275 const sal_Unicode *pEnd = pUnicode + rCommand.getLength();
276 bool bQuoted = false;
278 for ( ; pUnicode != pEnd; ++pUnicode )
280 if ( *pUnicode == '\\' )
282 ++pUnicode;
283 if ( pUnicode != pEnd )
285 if ( *pUnicode == 'n' )
286 aBuffer.appendAscii( "\n", 1 );
287 else
288 aBuffer.append( *pUnicode );
291 else if ( *pUnicode == '"' )
292 bQuoted = !bQuoted;
293 else if ( *pUnicode == ' ' && !bQuoted )
294 aList.push_back( aBuffer.makeStringAndClear() );
295 else
296 aBuffer.append( *pUnicode );
298 aList.push_back( aBuffer.makeStringAndClear() );
300 return aList;
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */