1 /*************************************************************************
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Sun Microsystems, Inc.
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Jan Holesovsky <kendy@openoffice.org>
60 ************************************************************************/
62 #ifndef _UNXCOMMANDTHREAD_HXX_
63 #include <UnxCommandThread.hxx>
66 #ifndef _UNXNOTIFYTHREAD_HXX_
67 #include <UnxNotifyThread.hxx>
70 #ifndef _RTL_USTRING_H_
71 #include <rtl/ustring.hxx>
74 #ifndef _RTL_USTRBUF_HXX_
75 #include <rtl/ustrbuf.hxx>
82 using namespace ::com::sun::star
;
84 //////////////////////////////////////////////////////////////////////////
85 // UnxFilePickerCommandThread
86 //////////////////////////////////////////////////////////////////////////
88 UnxFilePickerCommandThread::UnxFilePickerCommandThread( UnxFilePickerNotifyThread
*pNotifyThread
, int nReadFD
)
89 : m_pNotifyThread( pNotifyThread
),
94 UnxFilePickerCommandThread::~UnxFilePickerCommandThread()
98 sal_Bool SAL_CALL
UnxFilePickerCommandThread::result()
100 ::osl::MutexGuard
aGuard( m_aMutex
);
105 ::rtl::OUString SAL_CALL
UnxFilePickerCommandThread::getCurrentFilter()
107 ::osl::MutexGuard
aGuard( m_aMutex
);
109 return m_aGetCurrentFilter
;
112 ::rtl::OUString SAL_CALL
UnxFilePickerCommandThread::getDirectory()
114 ::osl::MutexGuard
aGuard( m_aMutex
);
116 return m_aGetDirectory
;
119 uno::Sequence
< ::rtl::OUString
> SAL_CALL
UnxFilePickerCommandThread::getFiles()
121 ::osl::MutexGuard
aGuard( m_aMutex
);
123 sal_Int32 nSize
= m_aGetFiles
.size();
124 uno::Sequence
< ::rtl::OUString
> aFiles( ( nSize
> 1 )? nSize
+ 1: nSize
);
127 aFiles
[0] = m_aGetFiles
.front();
128 else if ( nSize
> 1 )
130 // First entry in the sequence must be the dirname, the others are the
131 // filenames, so we have to rearrange the list...
133 ::rtl::OUString aFront
= m_aGetFiles
.front();
134 sal_Int32 nLastSlash
= aFront
.lastIndexOf( '/' );
136 aFiles
[0] = ( nLastSlash
>= 0 )? aFront
.copy( 0, nLastSlash
): ::rtl::OUString();
140 for ( ::std::list
< ::rtl::OUString
>::const_iterator it
= m_aGetFiles
.begin();
141 it
!= m_aGetFiles
.end(); ++it
, ++nIdx
)
143 sal_Int32 nLength
= (*it
).getLength() - nLastSlash
;
144 aFiles
[nIdx
] = ( nLength
>= 0 )? (*it
).copy( nLastSlash
, nLength
): ::rtl::OUString();
151 uno::Any SAL_CALL
UnxFilePickerCommandThread::getValue()
153 ::osl::MutexGuard
aGuard( m_aMutex
);
158 void SAL_CALL
UnxFilePickerCommandThread::run()
163 sal_Int32 nBufferSize
= 1024; // 1 is for testing, 1024 for real use
164 sal_Char
*pBuffer
= new sal_Char
[nBufferSize
];
165 sal_Char
*pBufferEnd
= pBuffer
+ nBufferSize
;
167 sal_Char
*pWhereToRead
= pBuffer
;
168 sal_Char
*pEntryBegin
= pBuffer
;
169 sal_Int32 nBytesRead
= 0;
170 sal_Bool bShouldExit
= sal_False
;
171 while ( !bShouldExit
&& ( nBytesRead
= read( m_nReadFD
, pWhereToRead
, pBufferEnd
- pWhereToRead
) ) > 0 )
173 sal_Bool bFoundNL
= sal_False
;
174 sal_Char
*pWhereToReadEnd
= pWhereToRead
+ nBytesRead
;
175 sal_Char
*pEntryEnd
= pWhereToRead
;
177 for ( ; pEntryEnd
< pWhereToReadEnd
&& *pEntryEnd
!= '\n'; ++pEntryEnd
)
180 if ( pEntryEnd
< pWhereToReadEnd
)
185 if ( strcmp( pEntryBegin
, "exited" ) == 0 )
186 bShouldExit
= sal_True
;
188 handleCommand( ::rtl::OUString( pEntryBegin
, pEntryEnd
- pEntryBegin
, RTL_TEXTENCODING_UTF8
)/*, bQuit*/ );
190 pEntryBegin
= pEntryEnd
+ 1;
192 } while ( pEntryEnd
< pWhereToReadEnd
);
196 if ( pEntryBegin
< pBufferEnd
)
197 memmove( pBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
201 // enlarge the buffer size
203 sal_Char
*pNewBuffer
= new sal_Char
[nBufferSize
];
204 if ( pEntryBegin
< pBufferEnd
)
205 memmove( pNewBuffer
, pEntryBegin
, pWhereToReadEnd
- pEntryBegin
);
208 pBuffer
= pNewBuffer
;
209 pBufferEnd
= pBuffer
+ nBufferSize
;
212 pWhereToRead
= pBuffer
+ ( pWhereToReadEnd
- pEntryBegin
);
213 pEntryBegin
= pBuffer
;
217 void SAL_CALL
UnxFilePickerCommandThread::handleCommand( const ::rtl::OUString
&rCommand
)
219 ::osl::MutexGuard
aGuard( m_aMutex
);
221 #if OSL_DEBUG_LEVEL > 0
222 ::std::cerr
<< "UnxFilePicker received: \"" <<
223 OUStringToOString( rCommand
, RTL_TEXTENCODING_ASCII_US
).getStr() << "\"" << ::std::endl
;
226 ::std::list
< ::rtl::OUString
> aList
= tokenize( rCommand
);
228 if ( aList
.size() == 0 )
231 ::rtl::OUString aCommandName
= aList
.front();
234 if ( aCommandName
.equalsAscii( "accept" ) )
236 m_aResult
= sal_True
;
237 m_aExecCondition
.set();
239 else if ( aCommandName
.equalsAscii( "reject" ) )
241 m_aResult
= sal_False
;
242 m_aExecCondition
.set();
244 else if ( aCommandName
.equalsAscii( "fileSelectionChanged" ) )
246 if ( m_pNotifyThread
)
247 m_pNotifyThread
->fileSelectionChanged();
249 else if ( aCommandName
.equalsAscii( "files" ) )
252 m_aGetFilesCondition
.set();
254 else if ( aCommandName
.equalsAscii( "value" ) )
256 ::rtl::OUString aType
;
257 if ( !aList
.empty() )
259 aType
= aList
.front();
263 if ( aType
.equalsAscii( "bool" ) )
265 sal_Bool bValue
= !aList
.empty() && aList
.front().equalsIgnoreAsciiCaseAscii( "true" );
267 m_aGetValue
<<= bValue
;
268 m_aGetValueCondition
.set();
270 else if ( aType
.equalsAscii( "int" ) )
272 sal_Int32 nValue
= 0;
273 if ( !aList
.empty() )
274 nValue
= aList
.front().toInt32();
276 m_aGetValue
<<= nValue
;
277 m_aGetValueCondition
.set();
279 else if ( aType
.equalsAscii( "string" ) )
281 ::rtl::OUString aValue
;
282 if ( !aList
.empty() )
283 aValue
= aList
.front();
285 m_aGetValue
<<= aValue
;
286 m_aGetValueCondition
.set();
288 else if ( aType
.equalsAscii( "stringList" ) )
290 uno::Sequence
< ::rtl::OUString
> aSequence( aList
.size() );
292 for ( ::std::list
< ::rtl::OUString
>::const_iterator it
= aList
.begin(); it
!= aList
.end(); ++it
, ++nIdx
)
293 aSequence
[nIdx
] = (*it
);
295 m_aGetValue
<<= aSequence
;
296 m_aGetValueCondition
.set();
300 m_aGetValue
= uno::Any();
301 m_aGetValueCondition
.set();
304 else if ( aCommandName
.equalsAscii( "currentFilter" ) )
306 m_aGetCurrentFilter
= aList
.empty()? ::rtl::OUString(): aList
.front();
307 m_aGetCurrentFilterCondition
.set();
309 else if ( aCommandName
.equalsAscii( "currentDirectory" ) )
311 m_aGetDirectory
= aList
.empty()? ::rtl::OUString(): aList
.front();
312 m_aGetDirectoryCondition
.set();
316 #if OSL_DEBUG_LEVEL > 0
317 ::std::cerr
<< "Unrecognized command: "
318 << OUStringToOString( aCommandName
, RTL_TEXTENCODING_ASCII_US
).getStr() << "\"" << ::std::endl
;
323 ::std::list
< ::rtl::OUString
> SAL_CALL
UnxFilePickerCommandThread::tokenize( const ::rtl::OUString
&rCommand
)
325 ::std::list
< ::rtl::OUString
> aList
;
326 ::rtl::OUStringBuffer
aBuffer( 1024 );
328 const sal_Unicode
*pUnicode
= rCommand
.getStr();
329 const sal_Unicode
*pEnd
= pUnicode
+ rCommand
.getLength();
330 sal_Bool bQuoted
= sal_False
;
332 for ( ; pUnicode
!= pEnd
; ++pUnicode
)
334 if ( *pUnicode
== '\\' )
337 if ( pUnicode
!= pEnd
)
339 if ( *pUnicode
== 'n' )
340 aBuffer
.appendAscii( "\n", 1 );
342 aBuffer
.append( *pUnicode
);
345 else if ( *pUnicode
== '"' )
347 else if ( *pUnicode
== ' ' && !bQuoted
)
348 aList
.push_back( aBuffer
.makeStringAndClear() );
350 aBuffer
.append( *pUnicode
);
352 aList
.push_back( aBuffer
.makeStringAndClear() );