Update ooo320-m1
[ooovba.git] / padmin / source / fontentry.cxx
blobf5e3f404a3228ff5fea19239cdaf3ed212b8ab4e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fontentry.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "fontentry.hxx"
32 #include "helper.hxx"
33 #include "padialog.hrc"
34 #include "vcl/strhelper.hxx"
36 #include "vcl/msgbox.hxx"
37 #include "vcl/fontmanager.hxx"
39 #include "osl/thread.h"
41 #include "tools/config.hxx"
43 using namespace padmin;
44 using namespace osl;
45 using namespace rtl;
46 using namespace psp;
48 #if 0
49 static void CreateAfmFile( const INetURLObject& rFontFile )
51 rtl_TextEncoding aEncoding = gsl_getSystemTextEncoding();
53 INetURLObject aFontMap( rFontFile.GetPath(), INET_PROT_FILE, INetURLObject::ENCODE_ALL );
54 aFontMap.Append( String( RTL_CONSTASCII_USTRINGPARAM( "Fontmap" ) ) );
55 INetURLObject aAfmFile( rFontFile );
56 aAfmFile.setExtension( String::CreateFromAscii( "afm", 3 ) );
59 SvFileStream aMap( aFontMap.PathToFileName(), STREAM_WRITE | STREAM_TRUNC );
60 if( aMap.IsOpen() )
62 SvFileStream aRead( rFontFile.GetFull(), STREAM_READ );
63 ByteString aLine;
64 ByteString aFullName;
65 int nPos;
66 while( ! aRead.IsEof() &&
67 ( nPos = aLine.Search( "/FullName" ) ) == STRING_NOTFOUND )
68 aRead.ReadLine( aLine );
69 aRead.Close();
70 if( nPos != STRING_NOTFOUND )
72 aLine.Erase( 0, nPos );
73 aFullName = aLine.GetToken( 1, '(' ).GetToken( 0, ')' );
75 aLine = '/';
76 aLine += aFullName;
77 aLine += ' ';
78 aLine += '(';
79 aLine += ByteString( rFontFile.GetName(), aEncoding );
80 aLine += ')';
81 aLine += ';';
83 aMap.WriteLine( aLine );
85 aMap.Close();
86 pid_t nPID = fork();
87 if( ! nPID )
89 INetURLObject aTmp( rFontFile );
90 aTmp.CutName();
91 ByteString aEnv( "GS_LIB=" );
92 aEnv += ByteString( aTmp.PathToFileName(), aEncoding );
93 putenv( const_cast<char*>(aEnv.GetBuffer()) );
95 int nDescr = open( ByteString( aAfmFile.PathToFileName(), aEncoding ).GetBuffer(),
96 O_CREAT | O_TRUNC| O_WRONLY,
97 00755 );
98 #if OSL_DEBUG_LEVEL > 1
99 if( nDescr < 0 )
100 fprintf( stderr, "open( %s ) failed because of %d\n", ByteString( aAfmFile.GetFull(), aEncoding ).GetBuffer(), errno );
101 #endif
102 if( dup2( nDescr, STDOUT_FILENO ) > 0 )
104 execlp( "gs",
105 "-q",
106 "-dNODISPLAY",
107 "--", "printafm.ps",
108 aFullName.GetBuffer(),
109 NULL );
111 #if OSL_DEBUG_LEVEL > 1
112 else
113 fprintf( stderr, "dup2( %d, %d ) failed because of %d\n", nDescr, STDOUT_FILENO, errno );
114 #endif
115 _exit( 0 );
117 if( nPID > 0 )
118 waitpid( nPID, NULL, 0 );
119 MetricConverter::ConvertAFM( aAfmFile );
122 struct stat aStat;
123 ByteString aSysFile( aAfmFile.PathToFileName(), aEncoding );
124 if( stat( aSysFile.GetBuffer(), &aStat ) || ! aStat.st_size )
125 unlink( aSysFile.GetBuffer() );
126 #if OSL_DEBUG_LEVEL > 1
127 fprintf( stderr, " %s\n", ! access( aSysFile.GetBuffer(), F_OK ) ? "success" : "failed" );
128 #endif
129 unlink( ByteString( aFontMap.PathToFileName(), aEncoding ).GetBuffer() );
132 #endif
134 FontNameDlg::FontNameDlg( Window *pParent ) :
135 ModalDialog( pParent, PaResId( RID_FONTNAMEDIALOG ) ),
136 m_aOKButton( this, PaResId( RID_FNTNM_BTN_OK ) ),
137 m_aRenameButton( this, PaResId( RID_FNTNM_BTN_RENAME ) ),
138 m_aRemoveButton( this, PaResId( RID_FNTNM_BTN_REMOVE ) ),
139 m_aImportButton( this, PaResId( RID_FNTNM_BTN_IMPORT ) ),
140 m_aFontBox( this, PaResId( RID_FNTNM_LB_FONTS ) ),
141 m_aFixedText( this, PaResId( RID_FNTNM_FIXED ) ),
142 m_aRenameString( PaResId( RID_FNTNM_STR_RENAME ) ),
143 m_aRenameTTCString( PaResId( RID_FNTNM_STR_TTCRENAME ) ),
144 m_aNoRenameString( PaResId( RID_FNTNM_STR_NOTRENAMABLE ) ),
145 m_rFontManager( PrintFontManager::get() )
147 FreeResource();
149 m_aFontBox.EnableMultiSelection( TRUE );
151 m_aOKButton.SetClickHdl( LINK( this, FontNameDlg, ClickBtnHdl ) );
152 m_aRenameButton.SetClickHdl( LINK( this, FontNameDlg, ClickBtnHdl ) );
153 m_aRemoveButton.SetClickHdl( LINK( this, FontNameDlg, ClickBtnHdl ) );
154 m_aImportButton.SetClickHdl( LINK( this, FontNameDlg, ClickBtnHdl ) );
155 m_aFontBox.setDelPressedLink( LINK( this, FontNameDlg, DelPressedHdl ) );
156 m_aFontBox.SetSelectHdl( LINK( this, FontNameDlg, SelectHdl ) );
158 init();
161 FontNameDlg::~FontNameDlg()
165 String FontNameDlg::fillFontEntry( FastPrintFontInfo& rInfo, const String& rFile, bool bAddRegular )
167 static String aThinTxt( PaResId( RID_TXT_FONT_THIN ) );
168 static String aUltraLightTxt( PaResId( RID_TXT_FONT_ULTRALIGHT ) );
169 static String aLightTxt( PaResId( RID_TXT_FONT_LIGHT ) );
170 static String aSemiLightTxt( PaResId( RID_TXT_FONT_SEMILIGHT ) );
171 static String aSemiBoldTxt( PaResId( RID_TXT_FONT_SEMIBOLD ) );
172 static String aBoldTxt( PaResId( RID_TXT_FONT_BOLD ) );
173 static String aUltraBoldTxt( PaResId( RID_TXT_FONT_ULTRABOLD ) );
175 static String aItalicTxt( PaResId( RID_TXT_FONT_ITALIC ) );
176 static String aObliqueTxt( PaResId( RID_TXT_FONT_OBLIQUE ) );
178 static String aUltraCondensedTxt( PaResId( RID_TXT_FONT_ULTRACONDENSED ) );
179 static String aExtraCondensedTxt( PaResId( RID_TXT_FONT_EXTRACONDENSED ) );
180 static String aCondensedTxt( PaResId( RID_TXT_FONT_CONDENSED ) );
181 static String aSemiCondensedTxt( PaResId( RID_TXT_FONT_SEMICONDENSED ) );
182 static String aSemiExpandedTxt( PaResId( RID_TXT_FONT_SEMIEXPANDED ) );
183 static String aExpandedTxt( PaResId( RID_TXT_FONT_EXPANDED ) );
184 static String aExtraExpandedTxt( PaResId( RID_TXT_FONT_EXTRAEXPANDED ) );
185 static String aUltraExpandedTxt( PaResId( RID_TXT_FONT_ULTRAEXPANDED ) );
187 static String aRegularTxt( PaResId( RID_TXT_FONT_REGULAR ) );
189 String aEntry( rInfo.m_aFamilyName );
190 bool bWeight = true, bItalic = true, bWidth = true;
191 switch( rInfo.m_eWeight )
193 case weight::Thin: aEntry.AppendAscii( ", " ); aEntry.Append( aThinTxt ); break;
194 case weight::UltraLight: aEntry.AppendAscii( ", " ); aEntry.Append( aUltraLightTxt ); break;
195 case weight::Light: aEntry.AppendAscii( ", " ); aEntry.Append( aLightTxt ); break;
196 case weight::SemiLight: aEntry.AppendAscii( ", " ); aEntry.Append( aSemiLightTxt ); break;
197 case weight::SemiBold: aEntry.AppendAscii( ", " ); aEntry.Append( aSemiBoldTxt ); break;
198 case weight::Bold: aEntry.AppendAscii( ", " ); aEntry.Append( aBoldTxt ); break;
199 case weight::UltraBold: aEntry.AppendAscii( ", " ); aEntry.Append( aUltraBoldTxt ); break;
200 default:
201 bWeight = false;
202 break;
204 switch( rInfo.m_eItalic )
206 case italic::Oblique: aEntry.AppendAscii( ", " ); aEntry.Append( aObliqueTxt ); break;
207 case italic::Italic: aEntry.AppendAscii( ", " ); aEntry.Append( aItalicTxt ); break;
208 default:
209 bItalic = false;
210 break;
212 switch( rInfo.m_eWidth )
214 case width::UltraCondensed: aEntry.AppendAscii( ", " ); aEntry.Append( aUltraCondensedTxt ); break;
215 case width::ExtraCondensed: aEntry.AppendAscii( ", " ); aEntry.Append( aExtraCondensedTxt ); break;
216 case width::Condensed: aEntry.AppendAscii( ", " ); aEntry.Append( aCondensedTxt ); break;
217 case width::SemiCondensed: aEntry.AppendAscii( ", " ); aEntry.Append( aSemiCondensedTxt ); break;
218 case width::SemiExpanded: aEntry.AppendAscii( ", " ); aEntry.Append( aSemiExpandedTxt ); break;
219 case width::Expanded: aEntry.AppendAscii( ", " ); aEntry.Append( aExpandedTxt ); break;
220 case width::ExtraExpanded: aEntry.AppendAscii( ", " ); aEntry.Append( aExtraExpandedTxt ); break;
221 case width::UltraExpanded: aEntry.AppendAscii( ", " ); aEntry.Append( aUltraExpandedTxt ); break;
222 default:
223 bWidth = false;
224 break;
227 if( bAddRegular && ! bItalic && ! bWidth && ! bWeight )
229 aEntry.AppendAscii( ", " );
230 aEntry.Append( aRegularTxt );
233 aEntry.AppendAscii( " (" );
234 aEntry.Append( rFile );
235 aEntry.AppendAscii( ")" );
236 return aEntry;
239 String FontNameDlg::fillFontEntry( const ::std::list< FastPrintFontInfo >& rInfos, const String& rFile )
241 String aEntry;
242 bool bFirst = true;
243 for( ::std::list< FastPrintFontInfo >::const_iterator it = rInfos.begin(); it != rInfos.end(); ++it )
245 if( ! bFirst )
246 aEntry.AppendAscii( " & " );
247 aEntry.Append( String( it->m_aFamilyName ) );
248 bFirst = false;
251 aEntry.AppendAscii( " (" );
252 aEntry.Append( rFile );
253 aEntry.AppendAscii( ")" );
254 return aEntry;
257 void FontNameDlg::init()
259 ::std::list< fontID > aFonts;
260 m_rFontManager.getFontList( aFonts );
261 m_aFontBox.Clear();
262 m_aRemoveButton.Enable( FALSE );
263 m_aRenameButton.Enable( FALSE );
265 ::std::hash_map< OUString, int, OUStringHash > aFamilies;
266 ::std::list< fontID >::iterator font_it;
267 for( font_it = aFonts.begin(); font_it != aFonts.end(); ++font_it )
269 if( m_rFontManager.isPrivateFontFile( *font_it ) )
271 const OUString& rFamily( m_rFontManager.getFontFamily( *font_it ) );
272 if( aFamilies.find( rFamily ) == aFamilies.end() )
273 aFamilies[rFamily] = 0;
274 aFamilies[rFamily]++;
278 for( font_it = aFonts.begin(); font_it != aFonts.end(); ++font_it )
280 if( m_rFontManager.isPrivateFontFile( *font_it ) )
282 OString aFile( m_rFontManager.getFontFileSysPath( *font_it ) );
283 int nLast = aFile.lastIndexOf( '/' );
284 if( nLast != -1 )
285 aFile = aFile.copy( nLast+1 );
287 FastPrintFontInfo aInfo;
288 m_rFontManager.getFontFastInfo( *font_it, aInfo );
289 std::list< fontID > aDups;
290 String aEntry;
291 if( m_rFontManager.getFileDuplicates( *font_it, aDups ) )
293 FastPrintFontInfo aDupInfo;
294 std::list< FastPrintFontInfo > aInfos;
295 aInfos.push_back( aInfo );
296 for( std::list< fontID >::iterator dup = aDups.begin(); dup != aDups.end(); ++dup )
298 m_rFontManager.getFontFastInfo( *dup, aDupInfo );
299 aInfos.push_back( aDupInfo );
300 aFonts.remove( *dup );
302 aEntry = fillFontEntry( aInfos, String( ByteString( aFile ), osl_getThreadTextEncoding() ) );
304 else
305 aEntry = fillFontEntry( aInfo, String( ByteString( aFile ), osl_getThreadTextEncoding() ), aFamilies[ aInfo.m_aFamilyName ] > 1 );
306 USHORT nEntry = m_aFontBox.InsertEntry( aEntry );
307 m_aFontBox.SetEntryData( nEntry, (void*)(*font_it) );
312 IMPL_LINK( FontNameDlg, SelectHdl, ListBox*, pBox )
314 if( pBox == &m_aFontBox )
316 BOOL bEnable = m_aFontBox.GetSelectEntryCount() ? TRUE : FALSE;
317 m_aRemoveButton.Enable( bEnable );
318 m_aRenameButton.Enable( bEnable );
320 return 0;
323 IMPL_LINK( FontNameDlg, DelPressedHdl, ListBox*, pBox )
325 if( pBox == &m_aFontBox && m_aRemoveButton.IsEnabled() )
327 ClickBtnHdl( &m_aRemoveButton );
329 return 0;
332 IMPL_LINK( FontNameDlg, ClickBtnHdl, Button*, pButton )
335 int i;
337 if( pButton == &m_aOKButton )
339 EndDialog();
341 else if( pButton == &m_aRemoveButton && AreYouSure( this, RID_QUERY_REMOVEFONTFROMLIST ) && m_aFontBox.GetSelectEntryCount() )
343 ::std::list< fontID > aRemoveIDs;
344 for( i = 0; i < m_aFontBox.GetSelectEntryCount(); i++ )
346 int nSelect = m_aFontBox.GetSelectEntryPos( i );
347 aRemoveIDs.push_back( (fontID)(sal_IntPtr)m_aFontBox.GetEntryData( nSelect ) );
349 m_rFontManager.removeFonts( aRemoveIDs );
350 init();
352 else if( pButton == &m_aImportButton )
354 FontImportDialog aDialog( this );
355 aDialog.Execute();
356 init();
358 else if( pButton == &m_aRenameButton && m_aFontBox.GetSelectEntryCount() )
360 for( i = 0; i < m_aFontBox.GetSelectEntryCount(); i++ )
362 fontID aFont = (fontID)(sal_IntPtr)m_aFontBox.GetEntryData( m_aFontBox.GetSelectEntryPos( i ) );
363 if( ! m_rFontManager.checkChangeFontPropertiesPossible( aFont ) )
365 String aErrorText( m_aNoRenameString );
366 aErrorText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), m_aFontBox.GetSelectEntry( i ) );
367 ErrorBox aBox( this, WB_OK | WB_DEF_OK, aErrorText );
368 aBox.Execute();
369 continue;
371 ::std::list< fontID > aDuplicates;
372 m_rFontManager.getFileDuplicates( aFont, aDuplicates );
373 aDuplicates.push_front( aFont );
374 int nFonts = aDuplicates.size();
375 for( int n = 0; n < nFonts; n++ )
377 aFont = aDuplicates.front();
378 aDuplicates.pop_front();
379 String aFamily( m_rFontManager.getFontFamily( aFont ) );
380 ::std::list< OUString > aAlternatives;
381 m_rFontManager.getAlternativeFamilyNames( aFont, aAlternatives );
382 ::std::list< String > aChoices;
383 while( aAlternatives.size() )
385 aChoices.push_back( aAlternatives.front() );
386 aAlternatives.pop_front();
388 String aQueryTxt( m_aRenameString );
389 if( nFonts > 1 )
391 aQueryTxt = m_aRenameTTCString;
392 aQueryTxt.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%d1" ) ), String::CreateFromInt32( n+1 ) );
393 aQueryTxt.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%d2" ) ), String::CreateFromInt32( nFonts ) );
395 aQueryTxt.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), aFamily );
396 QueryString aQuery( this, aQueryTxt, aFamily, aChoices );
397 if( aQuery.Execute() )
399 aFamily.SearchAndReplaceAll( '-', ' ' );
400 aFamily.SearchAndReplaceAll( '?', ' ' );
401 aFamily.SearchAndReplaceAll( '*', ' ' );
402 aFamily = WhitespaceToSpace( aFamily );
403 if( aFamily.Len() )
405 String aXLFD = m_rFontManager.getFontXLFD( aFont );
406 aXLFD.SetToken( 2, '-', aFamily );
407 m_rFontManager.changeFontProperties( aFont, aXLFD );
412 init();
414 return 0;
417 //--------------------------------------------------
419 FontImportDialog::FontImportDialog( Window* pParent ) :
420 ModalDialog( pParent, PaResId( RID_FONTIMPORT_DIALOG ) ),
421 m_aOKBtn( this, PaResId( RID_FIMP_BTN_OK ) ),
422 m_aCancelBtn( this, PaResId( RID_FIMP_BTN_CANCEL ) ),
423 m_aSelectAllBtn( this, PaResId( RID_FIMP_BTN_SELECTALL ) ),
424 m_aNewFontsBox( this, PaResId( RID_FIMP_BOX_NEWFONTS ) ),
425 m_aFromFL( this, PaResId( RID_FIMP_FL_FROM ) ),
426 m_aFromDirEdt( this, PaResId( RID_FIMP_EDT_FROM ) ),
427 m_aFromBtn( this, PaResId( RID_FIMP_BTN_FROM ) ),
428 m_aSubDirsBox( this, PaResId( RID_FIMP_BOX_SUBDIRS ) ),
429 m_aTargetOptFL( this, PaResId( RID_FIMP_FL_TARGETOPTS ) ),
430 m_aLinkOnlyBox( this, PaResId( RID_FIMP_BOX_LINKONLY ) ),
431 m_aFixedText( this, PaResId( RID_FIMP_TXT_HELP ) ),
432 m_bOverwriteAll( false ),
433 m_bOverwriteNone( false ),
434 m_pProgress( NULL ),
435 m_aImportOperation( PaResId( RID_FIMP_STR_IMPORTOP ) ),
436 m_aOverwriteQueryText( PaResId( RID_FIMP_STR_QUERYOVERWRITE ) ),
437 m_aOverwriteAllText( PaResId( RID_FIMP_STR_OVERWRITEALL ) ),
438 m_aOverwriteNoneText( PaResId( RID_FIMP_STR_OVERWRITENONE ) ),
439 m_aNoAfmText( PaResId( RID_FIMP_STR_NOAFM ) ),
440 m_aAfmCopyFailedText( PaResId( RID_FIMP_STR_AFMCOPYFAILED ) ),
441 m_aFontCopyFailedText( PaResId( RID_FIMP_STR_FONTCOPYFAILED ) ),
442 m_aNoWritableFontsDirText( PaResId( RID_FIMP_STR_NOWRITEABLEFONTSDIR ) ),
443 m_aFontsImportedText( PaResId( RID_FIMP_STR_NUMBEROFFONTSIMPORTED ) ),
444 m_rFontManager( ::psp::PrintFontManager::get() )
446 FreeResource();
448 m_aNewFontsBox.EnableMultiSelection( TRUE );
450 m_aOKBtn.SetClickHdl( LINK( this, FontImportDialog, ClickBtnHdl ) );
451 m_aSelectAllBtn.SetClickHdl( LINK( this, FontImportDialog, ClickBtnHdl ) );
452 m_aFromBtn.SetClickHdl( LINK( this, FontImportDialog, ClickBtnHdl ) );
453 m_aFromDirEdt.SetModifyHdl( LINK( this, FontImportDialog, ModifyHdl ) );
454 m_aRefreshTimer.SetTimeoutHdl( LINK( this, FontImportDialog, RefreshTimeoutHdl ) );
455 m_aRefreshTimer.SetTimeout( 2000 );
456 m_aLinkOnlyBox.Check( FALSE );
457 m_aSubDirsBox.Check( TRUE );
458 m_aSubDirsBox.SetToggleHdl( LINK( this, FontImportDialog, ToggleHdl ) );
460 Config& rPadminRC( getPadminRC() );
461 rPadminRC.SetGroup( "FontImport" );
462 m_aFromDirEdt.SetText( String( rPadminRC.ReadKey( "FromPath" ), RTL_TEXTENCODING_UTF8 ) );
463 RefreshTimeoutHdl( NULL );
466 FontImportDialog::~FontImportDialog()
468 Config& rPadminRC( getPadminRC() );
469 rPadminRC.SetGroup( "FontImport" );
470 rPadminRC.WriteKey( "FromPath", ByteString( m_aFromDirEdt.GetText(), RTL_TEXTENCODING_UTF8 ) );
473 void FontImportDialog::importFontsFailed( ::psp::PrintFontManager::ImportFontCallback::FailCondition eReason )
475 String aText;
476 switch( eReason )
478 case psp::PrintFontManager::ImportFontCallback::NoWritableDirectory:
479 aText = m_aNoWritableFontsDirText;
480 break;
481 default:
482 break;
484 ErrorBox aBox( m_pProgress ? (Window*)m_pProgress : (Window*)this, WB_OK | WB_DEF_OK, aText );
485 aBox.Execute();
488 void FontImportDialog::progress( const ::rtl::OUString& rFile )
490 m_pProgress->setValue( ++m_nFont );
491 m_pProgress->setFilename( rFile );
494 bool FontImportDialog::queryOverwriteFile( const ::rtl::OUString& rFile )
496 bool bRet = false;
498 if( m_bOverwriteNone )
499 return false;
500 if( m_bOverwriteAll )
501 return true;
503 String aText( m_aOverwriteQueryText );
504 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), rFile );
506 QueryBox aQueryBox( m_pProgress ? (Window*)m_pProgress : (Window*)this, WB_YES_NO | WB_DEF_NO, aText );
507 aQueryBox.AddButton( m_aOverwriteAllText, 20, 0 );
508 aQueryBox.AddButton( m_aOverwriteNoneText, 21, 0 );
510 int nResult = aQueryBox.Execute();
511 switch( nResult )
513 case BUTTONID_YES: bRet = true; break;
514 case BUTTONID_NO: bRet = false; break;
515 case 20: bRet = m_bOverwriteAll = true; break;
516 case 21: bRet = false; m_bOverwriteNone = true; break;
519 return bRet;
522 void FontImportDialog::importFontFailed( const ::rtl::OUString& rFile, ::psp::PrintFontManager::ImportFontCallback::FailCondition eReason )
524 String aText;
525 switch( eReason )
527 case psp::PrintFontManager::ImportFontCallback::NoAfmMetric:
528 aText = m_aNoAfmText;
529 break;
530 case psp::PrintFontManager::ImportFontCallback::AfmCopyFailed:
531 aText = m_aAfmCopyFailedText;
532 break;
533 case psp::PrintFontManager::ImportFontCallback::FontCopyFailed:
534 aText = m_aFontCopyFailedText;
535 break;
536 default:
537 break;
539 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%s" ) ), rFile );
540 ErrorBox aBox( m_pProgress ? (Window*)m_pProgress : (Window*)this, WB_OK | WB_DEF_OK, aText );
541 aBox.Execute();
544 bool FontImportDialog::isCanceled()
546 return m_pProgress->isCanceled();
549 IMPL_LINK( FontImportDialog, ModifyHdl, Edit*, EMPTYARG )
551 m_aRefreshTimer.Start();
552 return 0;
555 IMPL_LINK( FontImportDialog, RefreshTimeoutHdl, void*, EMPTYARG )
557 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
558 std::list< String > aFiles;
559 m_aNewFonts.clear();
560 OUString aDirectory( m_aFromDirEdt.GetText() );
561 FindFiles( aDirectory, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PFA;PFB;TTF;TTC" ) ), m_aSubDirsBox.IsChecked() );
562 OString aDir( OUStringToOString( aDirectory, aEncoding ) );
563 aDir += "/";
564 while( aFiles.begin() != aFiles.end() )
566 OString aFont( aDir );
567 aFont += OUStringToOString( aFiles.front(), aEncoding );
568 aFiles.pop_front();
569 ::std::list< FastPrintFontInfo > aInfos;
570 if( m_rFontManager.getImportableFontProperties( aFont, aInfos ) )
571 m_aNewFonts[ aFont ] = aInfos;
573 fillFontBox();
574 return 0;
577 void FontImportDialog::fillFontBox()
579 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
580 m_aNewFontsBox.Clear();
582 ::std::hash_map< OUString, int, OUStringHash > aFamilies;
583 ::std::hash_map< OString, ::std::list< FastPrintFontInfo >, OStringHash >::iterator it;
584 for( it = m_aNewFonts.begin(); it != m_aNewFonts.end(); ++it )
586 const OUString& rFamily( it->second.front().m_aFamilyName );
587 if( aFamilies.find( rFamily ) == aFamilies.end() )
588 aFamilies[rFamily] = 0;
589 aFamilies[rFamily]++;
592 for( it = m_aNewFonts.begin(); it != m_aNewFonts.end(); ++it )
594 OString aFileName( it->first.copy( it->first.lastIndexOf( '/' )+1 ) );
595 OUString aFile( OStringToOUString( aFileName, aEncoding ) );
596 String aEntry;
597 if( it->second.size() == 1 )
598 aEntry = FontNameDlg::fillFontEntry( it->second.front(), aFile, aFamilies[ it->second.front().m_aFamilyName ] > 1 );
599 else
600 aEntry = FontNameDlg::fillFontEntry( it->second, aFile );
601 USHORT nPos = m_aNewFontsBox.InsertEntry( aEntry );
602 m_aNewFontsBox.SetEntryData( nPos, (void*)&(it->first) );
606 void FontImportDialog::copyFonts()
608 ::std::list< OString > aFiles;
609 for( int i = 0; i < m_aNewFontsBox.GetSelectEntryCount(); i++ )
611 OString* pFile = (OString*)m_aNewFontsBox.GetEntryData( m_aNewFontsBox.GetSelectEntryPos( i ) );
612 aFiles.push_back( *pFile );
615 int nSuccess = 0;
616 if( aFiles.size() )
618 m_nFont = 0;
619 m_pProgress = new ProgressDialog( this );
620 m_pProgress->setRange( 0, aFiles.size() );
621 m_pProgress->startOperation( m_aImportOperation );
622 m_pProgress->Show( TRUE );
623 m_pProgress->setValue( 0 );
624 m_pProgress->Invalidate();
625 m_pProgress->Sync();
626 nSuccess = m_rFontManager.importFonts( aFiles, m_aLinkOnlyBox.IsChecked() ? true : false, this );
627 m_pProgress->Show( FALSE );
628 delete m_pProgress;
629 m_pProgress = NULL;
631 String aText( m_aFontsImportedText );
632 aText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%d" ) ), String::CreateFromInt32( nSuccess ) );
633 InfoBox aBox( this, aText );
634 aBox.Execute();
637 IMPL_LINK( FontImportDialog, ClickBtnHdl, Button*, pButton )
639 if( pButton == &m_aFromBtn )
641 String aPath( m_aFromDirEdt.GetText() );
642 if( chooseDirectory( aPath ) )
644 m_aFromDirEdt.SetText( aPath );
645 RefreshTimeoutHdl( NULL );
648 else if( pButton == &m_aOKBtn )
650 copyFonts();
651 EndDialog( 0 );
653 else if( pButton == &m_aSelectAllBtn )
655 m_aNewFontsBox.SetUpdateMode( FALSE );
656 for( int i = 0; i < m_aNewFontsBox.GetEntryCount(); i++ )
657 m_aNewFontsBox.SelectEntryPos( i, TRUE );
658 m_aNewFontsBox.SetUpdateMode( TRUE );
660 return 0;
663 IMPL_LINK( FontImportDialog, ToggleHdl, CheckBox*, pBox )
665 if( pBox == &m_aSubDirsBox )
667 RefreshTimeoutHdl( NULL );
670 return 0;