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 .
20 #include <UnxCommandThread.hxx>
21 #include <UnxNotifyThread.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
30 using namespace ::com::sun::star
;
32 // UnxFilePickerCommandThread
34 UnxFilePickerCommandThread::UnxFilePickerCommandThread( UnxFilePickerNotifyThread
*pNotifyThread
, int nReadFD
)
35 : m_pNotifyThread( pNotifyThread
),
40 UnxFilePickerCommandThread::~UnxFilePickerCommandThread()
44 bool SAL_CALL
UnxFilePickerCommandThread::result()
46 ::osl::MutexGuard
aGuard( m_aMutex
);
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
);
73 aFiles
[0] = m_aGetFiles
.front();
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();
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();
97 uno::Any SAL_CALL
UnxFilePickerCommandThread::getValue()
99 ::osl::MutexGuard
aGuard( m_aMutex
);
104 void SAL_CALL
UnxFilePickerCommandThread::run()
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
;
123 for ( ; pEntryEnd
< pWhereToReadEnd
&& *pEntryEnd
!= '\n'; ++pEntryEnd
)
126 if ( pEntryEnd
< pWhereToReadEnd
)
131 if ( strcmp( pEntryBegin
, "exited" ) == 0 )
134 handleCommand( OUString( pEntryBegin
, pEntryEnd
- pEntryBegin
, RTL_TEXTENCODING_UTF8
)/*, bQuit*/ );
136 pEntryBegin
= pEntryEnd
+ 1;
138 } while ( pEntryEnd
< pWhereToReadEnd
);
142 if ( pEntryBegin
< pBufferEnd
)
143 memmove( pBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
147 // enlarge the buffer size
149 sal_Char
*pNewBuffer
= new sal_Char
[nBufferSize
];
150 if ( pEntryBegin
< pBufferEnd
)
151 memcpy( pNewBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
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
;
172 ::std::list
< OUString
> aList
= tokenize( rCommand
);
177 OUString aCommandName
= aList
.front();
180 if ( aCommandName
== "accept" )
183 m_aExecCondition
.set();
185 else if ( aCommandName
== "reject" )
188 m_aExecCondition
.set();
190 else if ( aCommandName
== "fileSelectionChanged" )
192 if ( m_pNotifyThread
)
193 m_pNotifyThread
->fileSelectionChanged();
195 else if ( aCommandName
== "files" )
198 m_aGetFilesCondition
.set();
200 else if ( aCommandName
== "value" )
203 if ( !aList
.empty() )
205 aType
= aList
.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" )
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() );
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();
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();
262 #if OSL_DEBUG_LEVEL > 0
263 ::std::cerr
<< "Unrecognized command: "
264 << OUStringToOString( aCommandName
, RTL_TEXTENCODING_ASCII_US
).getStr() << "\"" << ::std::endl
;
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
== '\\' )
283 if ( pUnicode
!= pEnd
)
285 if ( *pUnicode
== 'n' )
286 aBuffer
.appendAscii( "\n", 1 );
288 aBuffer
.append( *pUnicode
);
291 else if ( *pUnicode
== '"' )
293 else if ( *pUnicode
== ' ' && !bQuoted
)
294 aList
.push_back( aBuffer
.makeStringAndClear() );
296 aBuffer
.append( *pUnicode
);
298 aList
.push_back( aBuffer
.makeStringAndClear() );
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */