nss: upgrade to release 3.73
[LibreOffice.git] / svtools / source / dialogs / prnsetup.cxx
blob6b95d7fb1bb79ba0c9e66d4e5707791485fb6bf8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <svtools/prnsetup.hxx>
21 #include <svtools/strings.hrc>
22 #include <svtools/svtresid.hxx>
23 #include <vcl/QueueInfo.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/print.hxx>
26 #include <vcl/event.hxx>
27 #include <sal/log.hxx>
29 void ImplFillPrnDlgListBox( const Printer* pPrinter,
30 weld::ComboBox* pBox, weld::Button* pPropBtn )
32 ImplFreePrnDlgListBox( pBox );
34 const std::vector<OUString>& rPrinters = Printer::GetPrinterQueues();
35 unsigned int nCount = rPrinters.size();
36 if ( nCount )
38 for( unsigned int i = 0; i < nCount; i++ )
39 pBox->append_text( rPrinters[i] );
40 pBox->set_active_text(pPrinter->GetName());
43 pBox->set_sensitive(nCount != 0);
44 pPropBtn->set_visible( pPrinter->HasSupport( PrinterSupport::SetupDialog ) );
48 void ImplFreePrnDlgListBox( weld::ComboBox* pBox, bool bClear )
50 if ( bClear )
51 pBox->clear();
55 Printer* ImplPrnDlgListBoxSelect( const weld::ComboBox* pBox, weld::Button* pPropBtn,
56 Printer const * pPrinter, Printer* pTempPrinterIn )
58 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
59 if ( pBox->get_active() != -1 )
61 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
62 if( pInfo)
64 if ( !pTempPrinter )
66 if ( (pPrinter->GetName() == pInfo->GetPrinterName()) &&
67 (pPrinter->GetDriverName() == pInfo->GetDriver()) )
68 pTempPrinter = VclPtr<Printer>::Create( pPrinter->GetJobSetup() );
69 else
70 pTempPrinter = VclPtr<Printer>::Create( *pInfo );
72 else
74 if ( (pTempPrinter->GetName() != pInfo->GetPrinterName()) ||
75 (pTempPrinter->GetDriverName() != pInfo->GetDriver()) )
77 pTempPrinter.disposeAndClear();
78 pTempPrinter = VclPtr<Printer>::Create( *pInfo );
82 pPropBtn->set_sensitive(pTempPrinter->HasSupport(PrinterSupport::SetupDialog));
84 else
85 pPropBtn->set_sensitive(false);
87 else
88 pPropBtn->set_sensitive(false);
90 return pTempPrinter;
94 Printer* ImplPrnDlgUpdatePrinter( Printer const * pPrinter, Printer* pTempPrinterIn )
96 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
97 OUString aPrnName;
98 if ( pTempPrinter )
99 aPrnName = pTempPrinter->GetName();
100 else
101 aPrnName = pPrinter->GetName();
103 if ( ! Printer::GetQueueInfo( aPrnName, false ) )
105 pTempPrinter.disposeAndClear();
106 pTempPrinter = VclPtr<Printer>::Create();
109 return pTempPrinter;
113 void ImplPrnDlgUpdateQueueInfo( const weld::ComboBox* pBox, QueueInfo& rInfo )
115 if ( pBox->get_active() != -1 )
117 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
118 if( pInfo )
119 rInfo = *pInfo;
124 static OUString ImplPrnDlgAddString(const OUString& rStr, const OUString& rAddStr)
126 OUString aStr(rStr);
127 if (!aStr.isEmpty())
128 aStr += "; " ;
129 return aStr + rAddStr;
133 static OUString ImplPrnDlgAddResString(const OUString& rStr, const char* pResId)
135 return ImplPrnDlgAddString(rStr, SvtResId(pResId));
139 OUString ImplPrnDlgGetStatusText( const QueueInfo& rInfo )
141 OUString aStr;
142 PrintQueueFlags nStatus = rInfo.GetStatus();
144 // Default-Printer
145 if ( !rInfo.GetPrinterName().isEmpty() &&
146 (rInfo.GetPrinterName() == Printer::GetDefaultPrinterName()) )
147 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DEFPRINTER );
149 // Status
150 if ( nStatus & PrintQueueFlags::Ready )
151 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_READY );
152 if ( nStatus & PrintQueueFlags::Paused )
153 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAUSED );
154 if ( nStatus & PrintQueueFlags::PendingDeletion )
155 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PENDING );
156 if ( nStatus & PrintQueueFlags::Busy )
157 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_BUSY );
158 if ( nStatus & PrintQueueFlags::Initializing )
159 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_INITIALIZING );
160 if ( nStatus & PrintQueueFlags::Waiting )
161 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WAITING );
162 if ( nStatus & PrintQueueFlags::WarmingUp )
163 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WARMING_UP );
164 if ( nStatus & PrintQueueFlags::Processing )
165 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PROCESSING );
166 if ( nStatus & PrintQueueFlags::Printing )
167 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PRINTING );
168 if ( nStatus & PrintQueueFlags::Offline )
169 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OFFLINE );
170 if ( nStatus & PrintQueueFlags::Error )
171 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_ERROR );
172 if ( nStatus & PrintQueueFlags::StatusUnknown )
173 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_SERVER_UNKNOWN );
174 if ( nStatus & PrintQueueFlags::PaperJam )
175 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_JAM );
176 if ( nStatus & PrintQueueFlags::PaperOut )
177 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_OUT );
178 if ( nStatus & PrintQueueFlags::ManualFeed )
179 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_MANUAL_FEED );
180 if ( nStatus & PrintQueueFlags::PaperProblem )
181 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_PROBLEM );
182 if ( nStatus & PrintQueueFlags::IOActive )
183 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_IO_ACTIVE );
184 if ( nStatus & PrintQueueFlags::OutputBinFull )
185 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUTPUT_BIN_FULL );
186 if ( nStatus & PrintQueueFlags::TonerLow )
187 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_TONER_LOW );
188 if ( nStatus & PrintQueueFlags::NoToner )
189 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_NO_TONER );
190 if ( nStatus & PrintQueueFlags::PagePunt )
191 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAGE_PUNT );
192 if ( nStatus & PrintQueueFlags::UserIntervention )
193 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_USER_INTERVENTION );
194 if ( nStatus & PrintQueueFlags::OutOfMemory )
195 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUT_OF_MEMORY );
196 if ( nStatus & PrintQueueFlags::DoorOpen )
197 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DOOR_OPEN );
198 if ( nStatus & PrintQueueFlags::PowerSave )
199 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_POWER_SAVE );
201 // Number of jobs
202 sal_uInt32 nJobs = rInfo.GetJobs();
203 if ( nJobs && (nJobs != QUEUE_JOBS_DONTKNOW) )
205 OUString aJobStr( SvtResId( STR_SVT_PRNDLG_JOBCOUNT ) );
206 OUString aJobs( OUString::number( nJobs ) );
207 aStr = ImplPrnDlgAddString(aStr, aJobStr.replaceAll("%d", aJobs));
210 return aStr;
213 PrinterSetupDialog::PrinterSetupDialog(weld::Window* pParent)
214 : GenericDialogController(pParent, "svt/ui/printersetupdialog.ui", "PrinterSetupDialog")
215 , m_xLbName(m_xBuilder->weld_combo_box("name"))
216 , m_xBtnProperties(m_xBuilder->weld_button("properties"))
217 , m_xBtnOptions(m_xBuilder->weld_button("options"))
218 , m_xFiStatus(m_xBuilder->weld_label("status"))
219 , m_xFiType(m_xBuilder->weld_label("type"))
220 , m_xFiLocation(m_xBuilder->weld_label("location"))
221 , m_xFiComment(m_xBuilder->weld_label("comment"))
223 m_xLbName->make_sorted();
225 // show options button only if link is set
226 m_xBtnOptions->hide();
228 mpPrinter = nullptr;
229 mpTempPrinter = nullptr;
231 maStatusTimer.SetTimeout( IMPL_PRINTDLG_STATUS_UPDATE );
232 maStatusTimer.SetInvokeHandler( LINK( this, PrinterSetupDialog, ImplStatusHdl ) );
233 m_xBtnProperties->connect_clicked( LINK( this, PrinterSetupDialog, ImplPropertiesHdl ) );
234 m_xLbName->connect_changed( LINK( this, PrinterSetupDialog, ImplChangePrinterHdl ) );
235 m_xDialog->connect_focus_in( LINK( this, PrinterSetupDialog, ImplGetFocusHdl ) );
236 Application::AddEventListener(LINK( this, PrinterSetupDialog, ImplDataChangedHdl ) );
239 PrinterSetupDialog::~PrinterSetupDialog()
241 Application::RemoveEventListener(LINK( this, PrinterSetupDialog, ImplDataChangedHdl ) );
242 ImplFreePrnDlgListBox(m_xLbName.get(), false);
245 void PrinterSetupDialog::SetOptionsHdl(const Link<weld::Button&, void>& rLink)
247 m_xBtnOptions->connect_clicked(rLink);
248 m_xBtnOptions->set_visible(rLink.IsSet());
251 void PrinterSetupDialog::ImplSetInfo()
253 const QueueInfo* pInfo = Printer::GetQueueInfo(m_xLbName->get_active_text(), true);
254 if ( pInfo )
256 m_xFiType->set_label( pInfo->GetDriver() );
257 m_xFiLocation->set_label( pInfo->GetLocation() );
258 m_xFiComment->set_label( pInfo->GetComment() );
259 m_xFiStatus->set_label( ImplPrnDlgGetStatusText( *pInfo ) );
261 else
263 const OUString aTempStr;
264 m_xFiType->set_label( aTempStr );
265 m_xFiLocation->set_label( aTempStr );
266 m_xFiComment->set_label( aTempStr );
267 m_xFiStatus->set_label( aTempStr );
271 IMPL_LINK_NOARG(PrinterSetupDialog, ImplStatusHdl, Timer *, void)
273 QueueInfo aInfo;
274 ImplPrnDlgUpdateQueueInfo(m_xLbName.get(), aInfo);
275 m_xFiStatus->set_label( ImplPrnDlgGetStatusText( aInfo ) );
279 IMPL_LINK_NOARG(PrinterSetupDialog, ImplPropertiesHdl, weld::Button&, void)
281 if ( !mpTempPrinter )
282 mpTempPrinter = VclPtr<Printer>::Create( mpPrinter->GetJobSetup() );
283 mpTempPrinter->Setup(m_xDialog.get());
286 IMPL_LINK_NOARG(PrinterSetupDialog, ImplChangePrinterHdl, weld::ComboBox&, void)
288 mpTempPrinter = ImplPrnDlgListBoxSelect(m_xLbName.get(), m_xBtnProperties.get(),
289 mpPrinter, mpTempPrinter);
290 ImplSetInfo();
293 IMPL_LINK(PrinterSetupDialog, ImplGetFocusHdl, weld::Widget&, rWidget, void)
295 if (rWidget.is_visible())
296 ImplStatusHdl(&maStatusTimer);
299 IMPL_LINK(PrinterSetupDialog, ImplDataChangedHdl, VclSimpleEvent&, rEvt, void)
301 VclEventId nEvent = rEvt.GetId();
302 if (nEvent != VclEventId::ApplicationDataChanged)
303 return;
305 DataChangedEvent* pData = static_cast<DataChangedEvent*>(static_cast<VclWindowEvent&>(rEvt).GetData());
306 if (!pData || pData->GetType() != DataChangedEventType::PRINTER)
307 return;
309 mpTempPrinter = ImplPrnDlgUpdatePrinter(mpPrinter, mpTempPrinter);
310 Printer* pPrn;
311 if (mpTempPrinter)
312 pPrn = mpTempPrinter;
313 else
314 pPrn = mpPrinter;
315 ImplFillPrnDlgListBox(pPrn, m_xLbName.get(), m_xBtnProperties.get());
316 ImplSetInfo();
319 short PrinterSetupDialog::run()
321 if ( !mpPrinter || mpPrinter->IsPrinting() || mpPrinter->IsJobActive() )
323 SAL_WARN( "svtools.dialogs", "PrinterSetupDialog::execute() - No Printer or printer is printing" );
324 return RET_CANCEL;
327 Printer::updatePrinters();
329 ImplFillPrnDlgListBox(mpPrinter, m_xLbName.get(), m_xBtnProperties.get());
330 ImplSetInfo();
331 maStatusTimer.Start();
333 // start dialog
334 short nRet = GenericDialogController::run();
336 // update data if the dialog was terminated with OK
337 if ( nRet == RET_OK && mpTempPrinter )
338 mpPrinter->SetPrinterProps( mpTempPrinter );
340 maStatusTimer.Stop();
342 return nRet;
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */