xref: /trunk/main/svtools/source/dialogs/prnsetup.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 #include <tools/debug.hxx>
31 #ifndef _VCL_PRINT_HXX
32 #include <vcl/print.hxx>
33 #endif
34 
35 #ifndef GCC
36 #endif
37 
38 #include <svtools/svtdata.hxx>
39 #include "prnsetup.hrc"
40 #include <svtools/prnsetup.hxx>
41 
42 // =======================================================================
43 
44 void ImplFillPrnDlgListBox( const Printer* pPrinter,
45                             ListBox* pBox, PushButton* pPropBtn )
46 {
47     ImplFreePrnDlgListBox( pBox );
48 
49     const std::vector<rtl::OUString>& rPrinters = Printer::GetPrinterQueues();
50     unsigned int nCount = rPrinters.size();
51     if ( nCount )
52     {
53         for( unsigned int i = 0; i < nCount; i++ )
54             pBox->InsertEntry( rPrinters[i] );
55         pBox->SelectEntry( pPrinter->GetName() );
56     }
57 
58     pBox->Enable( nCount != 0 );
59     pPropBtn->Show( pPrinter->HasSupport( SUPPORT_SETUPDIALOG ) );
60 }
61 
62 // -----------------------------------------------------------------------
63 
64 void ImplFreePrnDlgListBox( ListBox* pBox, sal_Bool bClear )
65 {
66     if ( bClear )
67         pBox->Clear();
68 }
69 
70 // -----------------------------------------------------------------------
71 
72 Printer* ImplPrnDlgListBoxSelect( ListBox* pBox, PushButton* pPropBtn,
73                                   Printer* pPrinter, Printer* pTempPrinter )
74 {
75     if ( pBox->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND )
76     {
77         const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->GetSelectEntry(), true );
78         if( pInfo)
79         {
80             if ( !pTempPrinter )
81             {
82                 if ( (pPrinter->GetName() == pInfo->GetPrinterName()) &&
83                      (pPrinter->GetDriverName() == pInfo->GetDriver()) )
84                     pTempPrinter = new Printer( pPrinter->GetJobSetup() );
85                 else
86                     pTempPrinter = new Printer( *pInfo );
87             }
88             else
89             {
90                 if ( (pTempPrinter->GetName() != pInfo->GetPrinterName()) ||
91                      (pTempPrinter->GetDriverName() != pInfo->GetDriver()) )
92                 {
93                     delete pTempPrinter;
94                     pTempPrinter = new Printer( *pInfo );
95                 }
96             }
97 
98             pPropBtn->Enable( pTempPrinter->HasSupport( SUPPORT_SETUPDIALOG ) );
99         }
100         else
101             pPropBtn->Disable();
102     }
103     else
104         pPropBtn->Disable();
105 
106     return pTempPrinter;
107 }
108 
109 // -----------------------------------------------------------------------
110 
111 Printer* ImplPrnDlgUpdatePrinter( Printer* pPrinter, Printer* pTempPrinter )
112 {
113     XubString aPrnName;
114     if ( pTempPrinter )
115         aPrnName = pTempPrinter->GetName();
116     else
117         aPrnName = pPrinter->GetName();
118 
119     if ( ! Printer::GetQueueInfo( aPrnName, false ) )
120     {
121         if ( pTempPrinter )
122             delete pTempPrinter;
123         pTempPrinter = new Printer;
124     }
125 
126     return pTempPrinter;
127 }
128 
129 // -----------------------------------------------------------------------
130 
131 void ImplPrnDlgUpdateQueueInfo( ListBox* pBox, QueueInfo& rInfo )
132 {
133     if ( pBox->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND )
134     {
135         const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->GetSelectEntry(), true );
136         if( pInfo )
137             rInfo = *pInfo;
138     }
139 }
140 
141 // -----------------------------------------------------------------------
142 
143 static void ImplPrnDlgAddString( XubString& rStr, const XubString& rAddStr )
144 {
145     if ( rStr.Len() )
146         rStr.AppendAscii( "; " );
147     rStr += rAddStr;
148 }
149 
150 // -----------------------------------------------------------------------
151 
152 static void ImplPrnDlgAddResString( XubString& rStr, sal_uInt16 nResId )
153 {
154     SvtResId aResId( nResId );
155     XubString aAddStr( aResId );
156     ImplPrnDlgAddString( rStr, aAddStr );
157 }
158 
159 // -----------------------------------------------------------------------
160 
161 XubString ImplPrnDlgGetStatusText( const QueueInfo& rInfo )
162 {
163     XubString   aStr;
164     sal_uLong       nStatus = rInfo.GetStatus();
165 
166     // Default-Printer
167     if ( rInfo.GetPrinterName().Len() &&
168          (rInfo.GetPrinterName() == Printer::GetDefaultPrinterName()) )
169         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DEFPRINTER );
170 
171     // Status
172     if ( nStatus & QUEUE_STATUS_READY )
173         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_READY );
174     if ( nStatus & QUEUE_STATUS_PAUSED )
175         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAUSED );
176     if ( nStatus & QUEUE_STATUS_PENDING_DELETION )
177         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PENDING );
178     if ( nStatus & QUEUE_STATUS_BUSY )
179         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_BUSY );
180     if ( nStatus & QUEUE_STATUS_INITIALIZING )
181         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_INITIALIZING );
182     if ( nStatus & QUEUE_STATUS_WAITING )
183         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WAITING );
184     if ( nStatus & QUEUE_STATUS_WARMING_UP )
185         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WARMING_UP );
186     if ( nStatus & QUEUE_STATUS_PROCESSING )
187         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PROCESSING );
188     if ( nStatus & QUEUE_STATUS_PRINTING )
189         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PRINTING );
190     if ( nStatus & QUEUE_STATUS_OFFLINE )
191         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OFFLINE );
192     if ( nStatus & QUEUE_STATUS_ERROR )
193         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_ERROR );
194     if ( nStatus & QUEUE_STATUS_SERVER_UNKNOWN )
195         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_SERVER_UNKNOWN );
196     if ( nStatus & QUEUE_STATUS_PAPER_JAM )
197         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_JAM );
198     if ( nStatus & QUEUE_STATUS_PAPER_OUT )
199         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_OUT );
200     if ( nStatus & QUEUE_STATUS_MANUAL_FEED )
201         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_MANUAL_FEED );
202     if ( nStatus & QUEUE_STATUS_PAPER_PROBLEM )
203         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_PROBLEM );
204     if ( nStatus & QUEUE_STATUS_IO_ACTIVE )
205         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_IO_ACTIVE );
206     if ( nStatus & QUEUE_STATUS_OUTPUT_BIN_FULL )
207         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUTPUT_BIN_FULL );
208     if ( nStatus & QUEUE_STATUS_TONER_LOW )
209         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_TONER_LOW );
210     if ( nStatus & QUEUE_STATUS_NO_TONER )
211         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_NO_TONER );
212     if ( nStatus & QUEUE_STATUS_PAGE_PUNT )
213         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAGE_PUNT );
214     if ( nStatus & QUEUE_STATUS_USER_INTERVENTION )
215         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_USER_INTERVENTION );
216     if ( nStatus & QUEUE_STATUS_OUT_OF_MEMORY )
217         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUT_OF_MEMORY );
218     if ( nStatus & QUEUE_STATUS_DOOR_OPEN )
219         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DOOR_OPEN );
220     if ( nStatus & QUEUE_STATUS_POWER_SAVE )
221         ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_POWER_SAVE );
222 
223     // Anzahl Jobs
224     sal_uLong nJobs = rInfo.GetJobs();
225     if ( nJobs && (nJobs != QUEUE_JOBS_DONTKNOW) )
226     {
227         XubString aJobStr( SvtResId( STR_SVT_PRNDLG_JOBCOUNT ) );
228         XubString aJobs( XubString::CreateFromInt32( nJobs ) );
229         aJobStr.SearchAndReplaceAscii( "%d", aJobs );
230         ImplPrnDlgAddString( aStr, aJobStr );
231     }
232 
233     return aStr;
234 }
235 
236 // =======================================================================
237 
238 PrinterSetupDialog::PrinterSetupDialog( Window* pWindow ) :
239     ModalDialog     ( pWindow, SvtResId( DLG_SVT_PRNDLG_PRNSETUPDLG ) ),
240     maFlPrinter     ( this, SvtResId( FL_PRINTER ) ),
241     maFtName        ( this, SvtResId( FT_NAME ) ),
242     maLbName        ( this, SvtResId( LB_NAMES ) ),
243     maBtnProperties ( this, SvtResId( BTN_PROPERTIES ) ),
244     maBtnOptions    ( this, SvtResId( BTN_OPTIONS ) ),
245     maFtStatus      ( this, SvtResId( FT_STATUS ) ),
246     maFiStatus      ( this, SvtResId( FI_STATUS ) ),
247     maFtType        ( this, SvtResId( FT_TYPE ) ),
248     maFiType        ( this, SvtResId( FI_TYPE ) ),
249     maFtLocation    ( this, SvtResId( FT_LOCATION ) ),
250     maFiLocation    ( this, SvtResId( FI_LOCATION ) ),
251     maFtComment     ( this, SvtResId( FT_COMMENT ) ),
252     maFiComment     ( this, SvtResId( FI_COMMENT ) ),
253     maFlSepButton   ( this, SvtResId( FL_SEPBUTTON ) ),
254     maBtnOK         ( this, SvtResId( BTN_OK ) ),
255     maBtnCancel     ( this, SvtResId( BTN_CANCEL ) ),
256     maBtnHelp       ( this, SvtResId( BTN_HELP ) )
257 {
258     FreeResource();
259 
260     // show options button only if link is set
261     maBtnOptions.Hide();
262 
263     mpPrinter       = NULL;
264     mpTempPrinter   = NULL;
265 
266     maStatusTimer.SetTimeout( IMPL_PRINTDLG_STATUS_UPDATE );
267     maStatusTimer.SetTimeoutHdl( LINK( this, PrinterSetupDialog, ImplStatusHdl ) );
268     maBtnProperties.SetClickHdl( LINK( this, PrinterSetupDialog, ImplPropertiesHdl ) );
269     maLbName.SetSelectHdl( LINK( this, PrinterSetupDialog, ImplChangePrinterHdl ) );
270 }
271 
272 // -----------------------------------------------------------------------
273 
274 PrinterSetupDialog::~PrinterSetupDialog()
275 {
276     ImplFreePrnDlgListBox( &maLbName, sal_False );
277     delete mpTempPrinter;
278 }
279 
280 // -----------------------------------------------------------------------
281 
282 void PrinterSetupDialog::SetOptionsHdl( const Link& rLink )
283 {
284     maBtnOptions.SetClickHdl( rLink );
285     maBtnOptions.Show( rLink.IsSet() );
286 }
287 
288 const Link& PrinterSetupDialog::GetOptionsHdl() const
289 {
290     return maBtnOptions.GetClickHdl();
291 }
292 
293 void PrinterSetupDialog::ImplSetInfo()
294 {
295     const QueueInfo* pInfo = Printer::GetQueueInfo(maLbName.GetSelectEntry(), true);
296     if ( pInfo )
297     {
298         maFiType.SetText( pInfo->GetDriver() );
299         maFiLocation.SetText( pInfo->GetLocation() );
300         maFiComment.SetText( pInfo->GetComment() );
301         maFiStatus.SetText( ImplPrnDlgGetStatusText( *pInfo ) );
302     }
303     else
304     {
305         XubString aTempStr;
306         maFiType.SetText( aTempStr );
307         maFiLocation.SetText( aTempStr );
308         maFiComment.SetText( aTempStr );
309         maFiStatus.SetText( aTempStr );
310     }
311 }
312 
313 // -----------------------------------------------------------------------
314 
315 IMPL_LINK( PrinterSetupDialog, ImplStatusHdl, Timer*, EMPTYARG )
316 {
317     QueueInfo aInfo;
318     ImplPrnDlgUpdateQueueInfo( &maLbName, aInfo );
319     maFiStatus.SetText( ImplPrnDlgGetStatusText( aInfo ) );
320 
321     return 0;
322 }
323 
324 // -----------------------------------------------------------------------
325 
326 IMPL_LINK( PrinterSetupDialog, ImplPropertiesHdl, void*, EMPTYARG )
327 {
328     if ( !mpTempPrinter )
329         mpTempPrinter = new Printer( mpPrinter->GetJobSetup() );
330     mpTempPrinter->Setup( this );
331 
332     return 0;
333 }
334 
335 // -----------------------------------------------------------------------
336 
337 IMPL_LINK( PrinterSetupDialog, ImplChangePrinterHdl, void*, EMPTYARG )
338 {
339     mpTempPrinter = ImplPrnDlgListBoxSelect( &maLbName, &maBtnProperties,
340                                              mpPrinter, mpTempPrinter );
341     ImplSetInfo();
342     return 0;
343 }
344 
345 // -----------------------------------------------------------------------
346 
347 long PrinterSetupDialog::Notify( NotifyEvent& rNEvt )
348 {
349     if ( (rNEvt.GetType() == EVENT_GETFOCUS) && IsReallyVisible() )
350         ImplStatusHdl( &maStatusTimer );
351 
352     return ModalDialog::Notify( rNEvt );
353 }
354 
355 // -----------------------------------------------------------------------
356 
357 void PrinterSetupDialog::DataChanged( const DataChangedEvent& rDCEvt )
358 {
359     if ( rDCEvt.GetType() == DATACHANGED_PRINTER )
360     {
361         mpTempPrinter = ImplPrnDlgUpdatePrinter( mpPrinter, mpTempPrinter );
362         Printer* pPrn;
363         if ( mpTempPrinter )
364             pPrn = mpTempPrinter;
365         else
366             pPrn = mpPrinter;
367         ImplFillPrnDlgListBox( pPrn, &maLbName, &maBtnProperties );
368         ImplSetInfo();
369     }
370 
371     ModalDialog::DataChanged( rDCEvt );
372 }
373 
374 // -----------------------------------------------------------------------
375 
376 short PrinterSetupDialog::Execute()
377 {
378     if ( !mpPrinter || mpPrinter->IsPrinting() || mpPrinter->IsJobActive() )
379     {
380         DBG_ERRORFILE( "PrinterSetupDialog::Execute() - No Printer or printer is printing" );
381         return sal_False;
382     }
383 
384     Printer::updatePrinters();
385 
386     ImplFillPrnDlgListBox( mpPrinter, &maLbName, &maBtnProperties );
387     ImplSetInfo();
388     maStatusTimer.Start();
389 
390     // Dialog starten
391     short nRet = ModalDialog::Execute();
392 
393     // Wenn Dialog mit OK beendet wurde, dann die Daten updaten
394     if ( nRet == sal_True )
395     {
396         if ( mpTempPrinter )
397             mpPrinter->SetPrinterProps( mpTempPrinter );
398     }
399 
400     maStatusTimer.Stop();
401 
402     return nRet;
403 }
404