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 .
24 #include "padialog.hrc"
25 #include "newppdlg.hxx"
26 #include "padialog.hxx"
27 #include "progress.hxx"
29 #include "vcl/ppdparser.hxx"
30 #include "vcl/helper.hxx"
31 #include "vcl/svapp.hxx"
32 #include "vcl/mnemonic.hxx"
34 #include "tools/config.hxx"
35 #include "tools/urlobj.hxx"
37 #include "osl/file.hxx"
41 #define PPDIMPORT_GROUP "PPDImport"
43 using namespace padmin
;
48 PPDImportDialog::PPDImportDialog( Window
* pParent
) :
49 ModalDialog( pParent
, PaResId( RID_PPDIMPORT_DLG
) ),
50 m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK
) ),
51 m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL
) ),
52 m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH
) ),
53 m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH
) ),
54 m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH
) ),
55 m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER
) ),
56 m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER
) ),
57 m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH
) ),
58 m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER
) ),
59 m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD
) )
63 String
aText( m_aDriverTxt
.GetText() );
64 aText
.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK
) );
65 m_aDriverTxt
.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText
) );
67 Config
& rConfig
= getPadminRC();
68 rConfig
.SetGroup( PPDIMPORT_GROUP
);
69 m_aPathBox
.SetText( OStringToOUString(rConfig
.ReadKey("LastDir"), RTL_TEXTENCODING_UTF8
) );
70 for (sal_Int32 i
= 0; i
< 11; ++i
)
72 OString
aEntry(rConfig
.ReadKey(OString::valueOf(i
)));
73 if (!aEntry
.isEmpty())
74 m_aPathBox
.InsertEntry(OStringToOUString(aEntry
, RTL_TEXTENCODING_UTF8
));
77 m_aOKBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
78 m_aCancelBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
79 m_aSearchBtn
.SetClickHdl( LINK( this, PPDImportDialog
, ClickBtnHdl
) );
80 m_aPathBox
.SetSelectHdl( LINK( this, PPDImportDialog
, SelectHdl
) );
81 m_aPathBox
.SetModifyHdl( LINK( this, PPDImportDialog
, ModifyHdl
) );
83 if( !m_aPathBox
.GetText().isEmpty() )
87 PPDImportDialog::~PPDImportDialog()
89 while( m_aDriverLB
.GetEntryCount() )
91 delete (String
*)m_aDriverLB
.GetEntryData( 0 );
92 m_aDriverLB
.RemoveEntry( 0 );
96 void PPDImportDialog::Import()
98 OUString
aImportPath( m_aPathBox
.GetText() );
100 Config
& rConfig
= getPadminRC();
101 rConfig
.SetGroup( PPDIMPORT_GROUP
);
102 rConfig
.WriteKey( "LastDir", OUStringToOString(aImportPath
, RTL_TEXTENCODING_UTF8
) );
104 int nEntries
= m_aPathBox
.GetEntryCount();
106 if( aImportPath
== m_aPathBox
.GetEntry( nEntries
) )
110 sal_Int32 nNextEntry
= rConfig
.ReadKey("NextEntry").toInt32();
111 rConfig
.WriteKey( OString::valueOf(nNextEntry
), OUStringToOString(aImportPath
, RTL_TEXTENCODING_UTF8
) );
112 nNextEntry
= nNextEntry
< 10 ? nNextEntry
+1 : 0;
113 rConfig
.WriteKey( "NextEntry", OString::valueOf(nNextEntry
) );
114 m_aPathBox
.InsertEntry( aImportPath
);
116 while( m_aDriverLB
.GetEntryCount() )
118 delete (String
*)m_aDriverLB
.GetEntryData( 0 );
119 m_aDriverLB
.RemoveEntry( 0 );
122 ProgressDialog
aProgress( Application::GetFocusWindow() );
123 aProgress
.startOperation( m_aLoadingPPD
);
125 ::std::list
< String
> aFiles
;
126 FindFiles( aImportPath
, aFiles
, OUString( "PS;PPD;PS.GZ;PPD.GZ" ), true );
129 aProgress
.setRange( 0, aFiles
.size() );
130 while( !aFiles
.empty() )
132 aProgress
.setValue( ++i
);
133 aProgress
.setFilename( aFiles
.front() );
134 INetURLObject
aPath( aImportPath
, INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
135 aPath
.Append( aFiles
.front() );
136 String aPrinterName
= PPDParser::getPPDPrinterName( aPath
.PathToFileName() );
139 if( ! aPrinterName
.Len() )
141 #if OSL_DEBUG_LEVEL > 1
142 fprintf( stderr
, "Warning: File %s has empty printer name.\n",
143 OUStringToOString( aPath
.PathToFileName(), osl_getThreadTextEncoding() ).getStr() );
148 sal_uInt16 nPos
= m_aDriverLB
.InsertEntry( aPrinterName
);
149 m_aDriverLB
.SetEntryData( nPos
, new String( aPath
.PathToFileName() ) );
153 IMPL_LINK( PPDImportDialog
, ClickBtnHdl
, PushButton
*, pButton
)
155 if( pButton
== &m_aCancelBtn
)
159 else if( pButton
== &m_aOKBtn
)
162 ::std::list
< OUString
> aToDirs
;
163 psp::getPrinterPathList( aToDirs
, PRINTER_PPDDIR
);
164 ::std::list
< OUString
>::iterator writeDir
= aToDirs
.begin();
165 m_aImportedFiles
.clear();
167 for( int i
= 0; i
< m_aDriverLB
.GetSelectEntryCount(); i
++ )
169 INetURLObject
aFile( *(String
*)m_aDriverLB
.GetEntryData(
170 m_aDriverLB
.GetSelectEntryPos( i
)
171 ), INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
172 OUString
aFromUni( aFile
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
176 INetURLObject
aToFile( *writeDir
, INET_PROT_FILE
, INetURLObject::ENCODE_ALL
);
177 aToFile
.Append( aFile
.GetName() );
178 OUString
aToUni( aToFile
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
179 if( ! File::copy( aFromUni
, aToUni
) )
181 m_aImportedFiles
.push_back( aToUni
);
185 } while( writeDir
!= aToDirs
.end() );
189 else if( pButton
== &m_aSearchBtn
)
191 OUString
aPath( m_aPathBox
.GetText() );
192 if( chooseDirectory( aPath
) )
194 m_aPathBox
.SetText( aPath
);
201 IMPL_LINK( PPDImportDialog
, SelectHdl
, ComboBox
*, pListBox
)
203 if( pListBox
== &m_aPathBox
)
210 IMPL_LINK( PPDImportDialog
, ModifyHdl
, ComboBox
*, pListBox
)
212 if( pListBox
== &m_aPathBox
)
214 OString
aDir(OUStringToOString(m_aPathBox
.GetText(), osl_getThreadTextEncoding()));
215 if (!access( aDir
.getStr(), F_OK
))
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */