Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <stdio.h>
21 : : #include <unistd.h>
22 : :
23 : : #include "helper.hxx"
24 : : #include "padialog.hrc"
25 : : #include "newppdlg.hxx"
26 : : #include "padialog.hxx"
27 : : #include "progress.hxx"
28 : :
29 : : #include "vcl/ppdparser.hxx"
30 : : #include "vcl/helper.hxx"
31 : : #include "vcl/svapp.hxx"
32 : : #include "vcl/mnemonic.hxx"
33 : :
34 : : #include "tools/config.hxx"
35 : : #include "tools/urlobj.hxx"
36 : :
37 : : #include "osl/file.hxx"
38 : :
39 : : #include <list>
40 : :
41 : : #define PPDIMPORT_GROUP "PPDImport"
42 : :
43 : : using namespace padmin;
44 : : using namespace psp;
45 : : using namespace osl;
46 : :
47 : : using ::rtl::OUString;
48 : : using ::rtl::OUStringToOString;
49 : :
50 : 0 : PPDImportDialog::PPDImportDialog( Window* pParent ) :
51 : : ModalDialog( pParent, PaResId( RID_PPDIMPORT_DLG ) ),
52 : : m_aOKBtn( this, PaResId( RID_PPDIMP_BTN_OK ) ),
53 : : m_aCancelBtn( this, PaResId( RID_PPDIMP_BTN_CANCEL ) ),
54 : : m_aPathTxt( this, PaResId( RID_PPDIMP_TXT_PATH ) ),
55 : : m_aPathBox( this, PaResId( RID_PPDIMP_LB_PATH ) ),
56 : : m_aSearchBtn( this, PaResId( RID_PPDIMP_BTN_SEARCH ) ),
57 : : m_aDriverTxt( this, PaResId( RID_PPDIMP_TXT_DRIVER ) ),
58 : : m_aDriverLB( this, PaResId( RID_PPDIMP_LB_DRIVER ) ),
59 : : m_aPathGroup( this, PaResId( RID_PPDIMP_GROUP_PATH ) ),
60 : : m_aDriverGroup( this, PaResId( RID_PPDIMP_GROUP_DRIVER ) ),
61 : 0 : m_aLoadingPPD( PaResId( RID_PPDIMP_STR_LOADINGPPD ) )
62 : : {
63 : 0 : FreeResource();
64 : :
65 : 0 : String aText( m_aDriverTxt.GetText() );
66 : 0 : aText.SearchAndReplaceAscii( "%s", Button::GetStandardText( BUTTON_OK ) );
67 : 0 : m_aDriverTxt.SetText( MnemonicGenerator::EraseAllMnemonicChars( aText ) );
68 : :
69 : 0 : Config& rConfig = getPadminRC();
70 : 0 : rConfig.SetGroup( PPDIMPORT_GROUP );
71 : 0 : m_aPathBox.SetText( rtl::OStringToOUString(rConfig.ReadKey("LastDir"), RTL_TEXTENCODING_UTF8) );
72 : 0 : for (sal_Int32 i = 0; i < 11; ++i)
73 : : {
74 : 0 : rtl::OString aEntry(rConfig.ReadKey(rtl::OString::valueOf(i)));
75 : 0 : if (!aEntry.isEmpty())
76 : 0 : m_aPathBox.InsertEntry(rtl::OStringToOUString(aEntry, RTL_TEXTENCODING_UTF8));
77 : 0 : }
78 : :
79 : 0 : m_aOKBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
80 : 0 : m_aCancelBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
81 : 0 : m_aSearchBtn.SetClickHdl( LINK( this, PPDImportDialog, ClickBtnHdl ) );
82 : 0 : m_aPathBox.SetSelectHdl( LINK( this, PPDImportDialog, SelectHdl ) );
83 : 0 : m_aPathBox.SetModifyHdl( LINK( this, PPDImportDialog, ModifyHdl ) );
84 : :
85 : 0 : if( m_aPathBox.GetText().Len() )
86 : 0 : Import();
87 : 0 : }
88 : :
89 : 0 : PPDImportDialog::~PPDImportDialog()
90 : : {
91 : 0 : while( m_aDriverLB.GetEntryCount() )
92 : : {
93 : 0 : delete (String*)m_aDriverLB.GetEntryData( 0 );
94 : 0 : m_aDriverLB.RemoveEntry( 0 );
95 : : }
96 : 0 : }
97 : :
98 : 0 : void PPDImportDialog::Import()
99 : : {
100 : 0 : String aImportPath( m_aPathBox.GetText() );
101 : :
102 : 0 : Config& rConfig = getPadminRC();
103 : 0 : rConfig.SetGroup( PPDIMPORT_GROUP );
104 : 0 : rConfig.WriteKey( "LastDir", rtl::OUStringToOString(aImportPath, RTL_TEXTENCODING_UTF8) );
105 : :
106 : 0 : int nEntries = m_aPathBox.GetEntryCount();
107 : 0 : while( nEntries-- )
108 : 0 : if( aImportPath == m_aPathBox.GetEntry( nEntries ) )
109 : 0 : break;
110 : 0 : if( nEntries < 0 )
111 : : {
112 : 0 : sal_Int32 nNextEntry = rConfig.ReadKey("NextEntry").toInt32();
113 : 0 : rConfig.WriteKey( rtl::OString::valueOf(nNextEntry), rtl::OUStringToOString(aImportPath, RTL_TEXTENCODING_UTF8) );
114 : 0 : nNextEntry = nNextEntry < 10 ? nNextEntry+1 : 0;
115 : 0 : rConfig.WriteKey( "NextEntry", rtl::OString::valueOf(nNextEntry) );
116 : 0 : m_aPathBox.InsertEntry( aImportPath );
117 : : }
118 : 0 : while( m_aDriverLB.GetEntryCount() )
119 : : {
120 : 0 : delete (String*)m_aDriverLB.GetEntryData( 0 );
121 : 0 : m_aDriverLB.RemoveEntry( 0 );
122 : : }
123 : :
124 : 0 : ProgressDialog aProgress( Application::GetFocusWindow() );
125 : 0 : aProgress.startOperation( m_aLoadingPPD );
126 : :
127 : 0 : ::std::list< String > aFiles;
128 : 0 : FindFiles( aImportPath, aFiles, String( RTL_CONSTASCII_USTRINGPARAM( "PS;PPD;PS.GZ;PPD.GZ" ) ), true );
129 : :
130 : 0 : int i = 0;
131 : 0 : aProgress.setRange( 0, aFiles.size() );
132 : 0 : while( !aFiles.empty() )
133 : : {
134 : 0 : aProgress.setValue( ++i );
135 : 0 : aProgress.setFilename( aFiles.front() );
136 : 0 : INetURLObject aPath( aImportPath, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
137 : 0 : aPath.Append( aFiles.front() );
138 : 0 : String aPrinterName = PPDParser::getPPDPrinterName( aPath.PathToFileName() );
139 : 0 : aFiles.pop_front();
140 : :
141 : 0 : if( ! aPrinterName.Len() )
142 : : {
143 : : #if OSL_DEBUG_LEVEL > 1
144 : : fprintf( stderr, "Warning: File %s has empty printer name.\n",
145 : : rtl::OUStringToOString( aPath.PathToFileName(), osl_getThreadTextEncoding() ).getStr() );
146 : : #endif
147 : 0 : continue;
148 : : }
149 : :
150 : 0 : sal_uInt16 nPos = m_aDriverLB.InsertEntry( aPrinterName );
151 : 0 : m_aDriverLB.SetEntryData( nPos, new String( aPath.PathToFileName() ) );
152 : 0 : }
153 : 0 : }
154 : :
155 : 0 : IMPL_LINK( PPDImportDialog, ClickBtnHdl, PushButton*, pButton )
156 : : {
157 : 0 : if( pButton == &m_aCancelBtn )
158 : : {
159 : 0 : EndDialog( 0 );
160 : : }
161 : 0 : else if( pButton == &m_aOKBtn )
162 : : {
163 : : // copy the files
164 : 0 : ::std::list< rtl::OUString > aToDirs;
165 : 0 : psp::getPrinterPathList( aToDirs, PRINTER_PPDDIR );
166 : 0 : ::std::list< rtl::OUString >::iterator writeDir = aToDirs.begin();
167 : 0 : m_aImportedFiles.clear();
168 : :
169 : 0 : for( int i = 0; i < m_aDriverLB.GetSelectEntryCount(); i++ )
170 : : {
171 : : INetURLObject aFile( *(String*)m_aDriverLB.GetEntryData(
172 : 0 : m_aDriverLB.GetSelectEntryPos( i )
173 : 0 : ), INET_PROT_FILE, INetURLObject::ENCODE_ALL );
174 : 0 : OUString aFromUni( aFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
175 : :
176 : 0 : do
177 : : {
178 : 0 : INetURLObject aToFile( *writeDir, INET_PROT_FILE, INetURLObject::ENCODE_ALL );
179 : 0 : aToFile.Append( aFile.GetName() );
180 : 0 : OUString aToUni( aToFile.GetMainURL(INetURLObject::DECODE_TO_IURI) );
181 : 0 : if( ! File::copy( aFromUni, aToUni ) )
182 : : {
183 : 0 : m_aImportedFiles.push_back( aToUni );
184 : : break;
185 : : }
186 : 0 : ++writeDir;
187 : 0 : } while( writeDir != aToDirs.end() );
188 : 0 : }
189 : 0 : EndDialog( 1 );
190 : : }
191 : 0 : else if( pButton == &m_aSearchBtn )
192 : : {
193 : 0 : String aPath( m_aPathBox.GetText() );
194 : 0 : if( chooseDirectory( aPath ) )
195 : : {
196 : 0 : m_aPathBox.SetText( aPath );
197 : 0 : Import();
198 : 0 : }
199 : : }
200 : 0 : return 0;
201 : : }
202 : :
203 : 0 : IMPL_LINK( PPDImportDialog, SelectHdl, ComboBox*, pListBox )
204 : : {
205 : 0 : if( pListBox == &m_aPathBox )
206 : : {
207 : 0 : Import();
208 : : }
209 : 0 : return 0;
210 : : }
211 : :
212 : 0 : IMPL_LINK( PPDImportDialog, ModifyHdl, ComboBox*, pListBox )
213 : : {
214 : 0 : if( pListBox == &m_aPathBox )
215 : : {
216 : 0 : rtl::OString aDir(rtl::OUStringToOString(m_aPathBox.GetText(), osl_getThreadTextEncoding()));
217 : 0 : if (!access( aDir.getStr(), F_OK))
218 : 0 : Import();
219 : : }
220 : 0 : return 0;
221 : : }
222 : :
223 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|