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 //////////////////////////////////////////////////////////////////////////
33 // UnxFilePickerCommandThread
34 //////////////////////////////////////////////////////////////////////////
36 UnxFilePickerCommandThread::UnxFilePickerCommandThread( UnxFilePickerNotifyThread
*pNotifyThread
, int nReadFD
)
37 : m_pNotifyThread( pNotifyThread
),
42 UnxFilePickerCommandThread::~UnxFilePickerCommandThread()
46 sal_Bool SAL_CALL
UnxFilePickerCommandThread::result()
48 ::osl::MutexGuard
aGuard( m_aMutex
);
53 OUString SAL_CALL
UnxFilePickerCommandThread::getCurrentFilter()
55 ::osl::MutexGuard
aGuard( m_aMutex
);
57 return m_aGetCurrentFilter
;
60 OUString SAL_CALL
UnxFilePickerCommandThread::getDirectory()
62 ::osl::MutexGuard
aGuard( m_aMutex
);
64 return m_aGetDirectory
;
67 uno::Sequence
< OUString
> SAL_CALL
UnxFilePickerCommandThread::getFiles()
69 ::osl::MutexGuard
aGuard( m_aMutex
);
71 sal_Int32 nSize
= m_aGetFiles
.size();
72 uno::Sequence
< OUString
> aFiles( ( nSize
> 1 )? nSize
+ 1: nSize
);
75 aFiles
[0] = m_aGetFiles
.front();
78 // First entry in the sequence must be the dirname, the others are the
79 // filenames, so we have to rearrange the list...
81 OUString aFront
= m_aGetFiles
.front();
82 sal_Int32 nLastSlash
= aFront
.lastIndexOf( '/' );
84 aFiles
[0] = ( nLastSlash
>= 0 )? aFront
.copy( 0, nLastSlash
): OUString();
88 for ( ::std::list
< OUString
>::const_iterator it
= m_aGetFiles
.begin();
89 it
!= m_aGetFiles
.end(); ++it
, ++nIdx
)
91 sal_Int32 nLength
= (*it
).getLength() - nLastSlash
;
92 aFiles
[nIdx
] = ( nLength
>= 0 )? (*it
).copy( nLastSlash
, nLength
): OUString();
99 uno::Any SAL_CALL
UnxFilePickerCommandThread::getValue()
101 ::osl::MutexGuard
aGuard( m_aMutex
);
106 void SAL_CALL
UnxFilePickerCommandThread::run()
111 sal_Int32 nBufferSize
= 1024; // 1 is for testing, 1024 for real use
112 sal_Char
*pBuffer
= new sal_Char
[nBufferSize
];
113 sal_Char
*pBufferEnd
= pBuffer
+ nBufferSize
;
115 sal_Char
*pWhereToRead
= pBuffer
;
116 sal_Char
*pEntryBegin
= pBuffer
;
117 sal_Int32 nBytesRead
= 0;
118 sal_Bool bShouldExit
= sal_False
;
119 while ( !bShouldExit
&& ( nBytesRead
= read( m_nReadFD
, pWhereToRead
, pBufferEnd
- pWhereToRead
) ) > 0 )
121 sal_Bool bFoundNL
= sal_False
;
122 sal_Char
*pWhereToReadEnd
= pWhereToRead
+ nBytesRead
;
123 sal_Char
*pEntryEnd
= pWhereToRead
;
125 for ( ; pEntryEnd
< pWhereToReadEnd
&& *pEntryEnd
!= '\n'; ++pEntryEnd
)
128 if ( pEntryEnd
< pWhereToReadEnd
)
133 if ( strcmp( pEntryBegin
, "exited" ) == 0 )
134 bShouldExit
= sal_True
;
136 handleCommand( OUString( pEntryBegin
, pEntryEnd
- pEntryBegin
, RTL_TEXTENCODING_UTF8
)/*, bQuit*/ );
138 pEntryBegin
= pEntryEnd
+ 1;
140 } while ( pEntryEnd
< pWhereToReadEnd
);
144 if ( pEntryBegin
< pBufferEnd
)
145 memmove( pBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
149 // enlarge the buffer size
151 sal_Char
*pNewBuffer
= new sal_Char
[nBufferSize
];
152 if ( pEntryBegin
< pBufferEnd
)
153 memcpy( pNewBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
156 pBuffer
= pNewBuffer
;
157 pBufferEnd
= pBuffer
+ nBufferSize
;
160 pWhereToRead
= pBuffer
+ ( pWhereToReadEnd
- pEntryBegin
);
161 pEntryBegin
= pBuffer
;
165 void SAL_CALL
UnxFilePickerCommandThread::handleCommand( const OUString
&rCommand
)
167 ::osl::MutexGuard
aGuard( m_aMutex
);
169 #if OSL_DEBUG_LEVEL > 0
170 ::std::cerr
<< "UnxFilePicker received: \"" <<
171 OUStringToOString( rCommand
, RTL_TEXTENCODING_ASCII_US
).getStr() << "\"" << ::std::endl
;
174 ::std::list
< OUString
> aList
= tokenize( rCommand
);
179 OUString aCommandName
= aList
.front();
182 if ( aCommandName
== "accept" )
184 m_aResult
= sal_True
;
185 m_aExecCondition
.set();
187 else if ( aCommandName
== "reject" )
189 m_aResult
= sal_False
;
190 m_aExecCondition
.set();
192 else if ( aCommandName
== "fileSelectionChanged" )
194 if ( m_pNotifyThread
)
195 m_pNotifyThread
->fileSelectionChanged();
197 else if ( aCommandName
== "files" )
200 m_aGetFilesCondition
.set();
202 else if ( aCommandName
== "value" )
205 if ( !aList
.empty() )
207 aType
= aList
.front();
211 if ( aType
== "bool" )
213 sal_Bool bValue
= !aList
.empty() && aList
.front().equalsIgnoreAsciiCase("true");
215 m_aGetValue
<<= bValue
;
216 m_aGetValueCondition
.set();
218 else if ( aType
== "int" )
220 sal_Int32 nValue
= 0;
221 if ( !aList
.empty() )
222 nValue
= aList
.front().toInt32();
224 m_aGetValue
<<= nValue
;
225 m_aGetValueCondition
.set();
227 else if ( aType
== "string" )
230 if ( !aList
.empty() )
231 aValue
= aList
.front();
233 m_aGetValue
<<= aValue
;
234 m_aGetValueCondition
.set();
236 else if ( aType
== "stringList" )
238 uno::Sequence
< OUString
> aSequence( aList
.size() );
240 for ( ::std::list
< OUString
>::const_iterator it
= aList
.begin(); it
!= aList
.end(); ++it
, ++nIdx
)
241 aSequence
[nIdx
] = (*it
);
243 m_aGetValue
<<= aSequence
;
244 m_aGetValueCondition
.set();
248 m_aGetValue
= uno::Any();
249 m_aGetValueCondition
.set();
252 else if ( aCommandName
== "currentFilter" )
254 m_aGetCurrentFilter
= aList
.empty()? OUString(): aList
.front();
255 m_aGetCurrentFilterCondition
.set();
257 else if ( aCommandName
== "currentDirectory" )
259 m_aGetDirectory
= aList
.empty()? OUString(): aList
.front();
260 m_aGetDirectoryCondition
.set();
264 #if OSL_DEBUG_LEVEL > 0
265 ::std::cerr
<< "Unrecognized command: "
266 << OUStringToOString( aCommandName
, RTL_TEXTENCODING_ASCII_US
).getStr() << "\"" << ::std::endl
;
271 ::std::list
< OUString
> SAL_CALL
UnxFilePickerCommandThread::tokenize( const OUString
&rCommand
)
273 ::std::list
< OUString
> aList
;
274 OUStringBuffer
aBuffer( 1024 );
276 const sal_Unicode
*pUnicode
= rCommand
.getStr();
277 const sal_Unicode
*pEnd
= pUnicode
+ rCommand
.getLength();
278 sal_Bool bQuoted
= sal_False
;
280 for ( ; pUnicode
!= pEnd
; ++pUnicode
)
282 if ( *pUnicode
== '\\' )
285 if ( pUnicode
!= pEnd
)
287 if ( *pUnicode
== 'n' )
288 aBuffer
.appendAscii( "\n", 1 );
290 aBuffer
.append( *pUnicode
);
293 else if ( *pUnicode
== '"' )
295 else if ( *pUnicode
== ' ' && !bQuoted
)
296 aList
.push_back( aBuffer
.makeStringAndClear() );
298 aBuffer
.append( *pUnicode
);
300 aList
.push_back( aBuffer
.makeStringAndClear() );
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */