xref: /AOO41X/main/svx/source/dialog/pfiledlg.cxx (revision f6e50924346d0b8c0b07c91832a97665dd718b0c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 // include ---------------------------------------------------------------
28 #include <sfx2/docfile.hxx>
29 #include <com/sun/star/plugin/PluginDescription.hpp>
30 #include <com/sun/star/plugin/XPluginManager.hpp>
31 
32 #include <comphelper/processfactory.hxx>
33 
34 #include "svx/pfiledlg.hxx"
35 #include <svx/dialogs.hrc>
36 
37 #include <svx/dialmgr.hxx>
38 #include <list>
39 
40 using namespace ::rtl;
41 using namespace ::com::sun::star;
42 
43 sal_Char __READONLY_DATA sAudio[] = "audio";
44 sal_Char __READONLY_DATA sVideo[] = "video";
45 
46 /*************************************************************************
47 |*
48 |* Filedialog to insert Plugin-Fileformats
49 |*
50 \************************************************************************/
51 
Execute()52 ErrCode SvxPluginFileDlg::Execute()
53 {
54     return maFileDlg.Execute();
55 }
56 
GetPath() const57 String SvxPluginFileDlg::GetPath() const
58 {
59     return maFileDlg.GetPath();
60 }
61 
SvxPluginFileDlg(Window *,sal_uInt16 nKind)62 SvxPluginFileDlg::SvxPluginFileDlg (Window *, sal_uInt16 nKind ) :
63     maFileDlg(SFXWB_INSERT)
64 {
65     // set title of the dialogwindow
66     switch (nKind)
67     {
68         case SID_INSERT_SOUND :
69         {
70             maFileDlg.SetTitle(SVX_RESSTR(STR_INSERT_SOUND_TITLE));
71         }
72         break;
73         case SID_INSERT_VIDEO :
74         {
75             maFileDlg.SetTitle(SVX_RESSTR(STR_INSERT_VIDEO_TITLE));
76         }
77         break;
78     }
79 
80     // fill the filterlist of the filedialog with data of installed plugins
81     uno::Reference< lang::XMultiServiceFactory >  xMgr( ::comphelper::getProcessServiceFactory() );
82 
83     if( xMgr.is() )
84     {
85         uno::Reference< plugin::XPluginManager > rPluginManager( xMgr->createInstance(
86             OUString::createFromAscii( "com.sun.star.plugin.PluginManager" ) ), uno::UNO_QUERY );
87         if ( rPluginManager.is() )
88         {
89             const uno::Sequence<plugin::PluginDescription > aSeq( rPluginManager->getPluginDescriptions() );
90             const plugin::PluginDescription* pDescription = aSeq.getConstArray();
91             sal_Int32 nAnzahlPlugins = rPluginManager->getPluginDescriptions().getLength();
92 
93             std::list< String > aPlugNames;
94             std::list< String > aPlugExtensions;
95             std::list< String >::iterator j;
96             std::list< String >::iterator k;
97             std::list< String >::const_iterator end;
98 
99             for ( int i = 0; i < nAnzahlPlugins; i++ )
100             {
101                 String aStrPlugMIMEType( pDescription[i].Mimetype );
102                 String aStrPlugName( pDescription[i].Description );
103                 String aStrPlugExtension( pDescription[i].Extension );
104 
105                 aStrPlugMIMEType.ToLowerAscii();
106                 aStrPlugExtension.ToLowerAscii();
107 
108                 if ( ( nKind == SID_INSERT_SOUND && aStrPlugMIMEType.SearchAscii ( sAudio ) == 0 ) ||
109                      ( nKind == SID_INSERT_VIDEO && aStrPlugMIMEType.SearchAscii ( sVideo ) == 0 ) )
110                 {
111                     // extension already in the filterlist of the filedlg ?
112                     sal_Bool bAlreadyExist = sal_False;
113                     for ( j = aPlugExtensions.begin(), end = aPlugExtensions.end(); j != end && !bAlreadyExist; ++j )
114                     {
115                         bAlreadyExist = (j->Search( aStrPlugExtension ) != STRING_NOTFOUND );
116                     }
117 
118                     if ( !bAlreadyExist )
119                     {
120                         // filterdescription already there?
121                         // (then append the new extension to the existing filter)
122                         int nfound = -1;
123                         for ( j = aPlugNames.begin(),
124                                   k = aPlugExtensions.begin(),
125                                   end = aPlugNames.end();
126                               j != end && nfound != 0;  )
127                         {
128                             if ( ( nfound = j->Search( aStrPlugName ) ) == 0 )
129                             {
130                                 if ( aStrPlugExtension.Len() > 0 )
131                                     aStrPlugExtension.Insert( sal_Unicode( ';' ) );
132                                 aStrPlugExtension.Insert( *k );
133 
134                                 // remove old entry, increment (iterators are invalid thereafter, thus the postincrement)
135                                 aPlugNames.erase(j++); aPlugExtensions.erase(k++);
136 
137                                 // update end iterator (which may be invalid, too!)
138                                 end = aPlugNames.end();
139                             }
140                             else
141                             {
142                                 // next element
143                                 ++j; ++k;
144                             }
145                         }
146 
147                         // build filterdescription
148                         aStrPlugName.AppendAscii( RTL_CONSTASCII_STRINGPARAM( "  (" ) );
149                         aStrPlugName.Append( aStrPlugExtension );
150                         aStrPlugName.AppendAscii( RTL_CONSTASCII_STRINGPARAM( ")" ) );
151 
152                         // use a own description for the video-formate avi, mov and mpeg
153                         // the descriptions of these MIME-types are not very meaningful
154                         const sal_Char sAVI[] = "*.avi";
155                         const sal_Char sMOV[] = "*.mov";
156                         const sal_Char sMPG[] = "*.mpg";
157                         const sal_Char sMPE[] = "*.mpe";
158                         const sal_Char sMPEG[] = "*.mpeg";
159 
160                         if ( aStrPlugExtension.EqualsIgnoreCaseAscii( sAVI ) )
161                             aStrPlugName = SVX_RESSTR( STR_INSERT_VIDEO_EXTFILTER_AVI );
162                         else if ( aStrPlugExtension.EqualsIgnoreCaseAscii( sMOV ) )
163                             aStrPlugName = SVX_RESSTR( STR_INSERT_VIDEO_EXTFILTER_MOV );
164                         else if ( aStrPlugExtension.SearchAscii( sMPG ) != STRING_NOTFOUND ||
165                                   aStrPlugExtension.SearchAscii( sMPE ) != STRING_NOTFOUND ||
166                                   aStrPlugExtension.SearchAscii( sMPEG ) != STRING_NOTFOUND )
167                             aStrPlugName = SVX_RESSTR(STR_INSERT_VIDEO_EXTFILTER_MPEG);
168 
169                         aPlugNames.push_back( aStrPlugName );
170                         aPlugExtensions.push_back( aStrPlugExtension );
171                     }
172                 }
173             }
174 
175             // add filter to dialog
176             for ( j = aPlugNames.begin(),
177                       k = aPlugExtensions.begin(),
178                       end = aPlugNames.end();
179                   j != end; ++j, ++k )
180             {
181                 maFileDlg.AddFilter( *j, *k );
182             }
183         }
184     }
185 
186     // add the All-Filter
187     String aAllFilter( ResId( STR_EXTFILTER_ALL, DIALOG_MGR() ) );
188     maFileDlg.AddFilter( aAllFilter, UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "*.*" ) ) );
189 
190     // and activate him
191     maFileDlg.SetCurrentFilter( aAllFilter );
192 }
193 
194 /*************************************************************************
195 |*
196 |* Dtor
197 |*
198 \************************************************************************/
199 
~SvxPluginFileDlg()200 SvxPluginFileDlg::~SvxPluginFileDlg()
201 {
202 }
203 
204 /*************************************************************************
205 |*
206 |* Plugins available for the the MIME-Typ in nKind
207 |* (whith nKind = SID_INSERT_SOUND for MIME-Type audio
208 |*                SID_INSERT_VIDEO for MIME-Type video
209 |*
210 \************************************************************************/
211 
212 #define PFDLG_CHECKED_SOUND     0x0001
213 #define PFDLG_CHECKED_VIDEO     0x0002
214 #define PFDLG_FOUND_SOUND       0x0004
215 #define PFDLG_FOUND_VIDEO       0x0008
216 
IsAvailable(sal_uInt16 nKind)217 bool SvxPluginFileDlg::IsAvailable (sal_uInt16 nKind)
218 {
219     static sal_uInt16 nCheck = 0;
220 
221     if ( nKind == SID_INSERT_SOUND && ( nCheck & PFDLG_CHECKED_SOUND ) )
222         return (nCheck & PFDLG_FOUND_SOUND) != 0;
223     if ( nKind == SID_INSERT_VIDEO && ( nCheck & PFDLG_CHECKED_VIDEO ) )
224         return (nCheck & PFDLG_FOUND_VIDEO) != 0;
225 
226     bool bFound = false;
227     uno::Reference< lang::XMultiServiceFactory >  xMgr( ::comphelper::getProcessServiceFactory() );
228 
229     if( xMgr.is() )
230     {
231         uno::Reference< plugin::XPluginManager >  rPluginManager = uno::Reference< plugin::XPluginManager > ( xMgr->createInstance( OUString::createFromAscii( "com.sun.star.plugin.PluginManager" ) ), uno::UNO_QUERY );
232         if( rPluginManager.is() )
233         {
234             const uno::Sequence<plugin::PluginDescription > aSeq( rPluginManager->getPluginDescriptions() );
235             const plugin::PluginDescription* pDescription = aSeq.getConstArray();
236             sal_Int32 nAnzahlPlugins = rPluginManager->getPluginDescriptions().getLength();
237 
238             for ( sal_uInt16 i = 0; i < nAnzahlPlugins && !bFound; ++i )
239             {
240                 String aStrPlugMIMEType( pDescription[i].Mimetype );
241                 switch (nKind)
242                 {
243                     case SID_INSERT_SOUND :
244                     {
245                         nCheck |= PFDLG_CHECKED_SOUND;
246 
247                         if( aStrPlugMIMEType.SearchAscii( sAudio ) == 0 )
248                         {
249                             bFound = true;
250                             nCheck |= PFDLG_FOUND_SOUND;
251                         }
252                     }
253                     break;
254                     case SID_INSERT_VIDEO :
255                     {
256                         nCheck |= PFDLG_CHECKED_VIDEO;
257 
258                         if (aStrPlugMIMEType.SearchAscii( sVideo ) == 0)
259                         {
260                             bFound = true;
261                             nCheck |= PFDLG_FOUND_VIDEO;
262                         }
263                     }
264                     break;
265                 }
266             }
267         }
268     }
269 
270     return bFound;
271 }
272 
SetContext(sfx2::FileDialogHelper::Context _eNewContext)273 void SvxPluginFileDlg::SetContext( sfx2::FileDialogHelper::Context _eNewContext )
274 {
275     maFileDlg.SetContext( _eNewContext );
276 }
277