cid#1640468 Dereference after null check
[LibreOffice.git] / svtools / source / dialogs / prnsetup.cxx
blob9d71832b3513c44e91950ee96b373e0ad277137b
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 <sal/config.h>
22 #include <string_view>
24 #include <svtools/prnsetup.hxx>
25 #include <svtools/strings.hrc>
26 #include <svtools/svtresid.hxx>
27 #include <vcl/QueueInfo.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/print.hxx>
30 #include <vcl/event.hxx>
31 #include <sal/log.hxx>
33 void ImplFillPrnDlgListBox( const Printer* pPrinter,
34 weld::ComboBox* pBox, weld::Button* pPropBtn )
36 ImplFreePrnDlgListBox( pBox );
38 const std::vector<OUString>& rPrinters = Printer::GetPrinterQueues();
39 unsigned int nCount = rPrinters.size();
40 if ( nCount )
42 for( unsigned int i = 0; i < nCount; i++ )
43 pBox->append_text( rPrinters[i] );
44 pBox->set_active_text(pPrinter->GetName());
47 pBox->set_sensitive(nCount != 0);
48 pPropBtn->set_visible( pPrinter->HasSupport( PrinterSupport::SetupDialog ) );
52 void ImplFreePrnDlgListBox( weld::ComboBox* pBox, bool bClear )
54 if ( bClear )
55 pBox->clear();
59 Printer* ImplPrnDlgListBoxSelect( const weld::ComboBox* pBox, weld::Button* pPropBtn,
60 Printer const * pPrinter, Printer* pTempPrinterIn )
62 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
63 if ( pBox->get_active() != -1 )
65 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
66 if( pInfo)
68 if ( !pTempPrinter )
70 if ( (pPrinter->GetName() == pInfo->GetPrinterName()) &&
71 (pPrinter->GetDriverName() == pInfo->GetDriver()) )
72 pTempPrinter = VclPtr<Printer>::Create( pPrinter->GetJobSetup() );
73 else
74 pTempPrinter = VclPtr<Printer>::Create( *pInfo );
76 else
78 if ( (pTempPrinter->GetName() != pInfo->GetPrinterName()) ||
79 (pTempPrinter->GetDriverName() != pInfo->GetDriver()) )
81 pTempPrinter.disposeAndClear();
82 pTempPrinter = VclPtr<Printer>::Create( *pInfo );
86 pPropBtn->set_sensitive(pTempPrinter->HasSupport(PrinterSupport::SetupDialog));
88 else
89 pPropBtn->set_sensitive(false);
91 else
92 pPropBtn->set_sensitive(false);
94 return pTempPrinter;
98 Printer* ImplPrnDlgUpdatePrinter( Printer const * pPrinter, Printer* pTempPrinterIn )
100 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
101 OUString aPrnName;
102 if ( pTempPrinter )
103 aPrnName = pTempPrinter->GetName();
104 else
105 aPrnName = pPrinter->GetName();
107 if ( ! Printer::GetQueueInfo( aPrnName, false ) )
109 pTempPrinter.disposeAndClear();
110 pTempPrinter = VclPtr<Printer>::Create();
113 return pTempPrinter;
117 void ImplPrnDlgUpdateQueueInfo( const weld::ComboBox* pBox, QueueInfo& rInfo )
119 if ( pBox->get_active() != -1 )
121 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
122 if( pInfo )
123 rInfo = *pInfo;
128 static OUString ImplPrnDlgAddString(const OUString& rStr, std::u16string_view rAddStr)
130 OUString aStr(rStr);
131 if (!aStr.isEmpty())
132 aStr += "; " ;
133 return aStr + rAddStr;
137 static OUString ImplPrnDlgAddResString(const OUString& rStr, TranslateId pResId)
139 return ImplPrnDlgAddString(rStr, SvtResId(pResId));
143 OUString ImplPrnDlgGetStatusText( const QueueInfo& rInfo )
145 OUString aStr;
146 PrintQueueFlags nStatus = rInfo.GetStatus();
148 // Default-Printer
149 if ( !rInfo.GetPrinterName().isEmpty() &&
150 (rInfo.GetPrinterName() == Printer::GetDefaultPrinterName()) )
151 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DEFPRINTER );
153 // Status
154 if ( nStatus & PrintQueueFlags::Ready )
155 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_READY );
156 if ( nStatus & PrintQueueFlags::Paused )
157 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAUSED );
158 if ( nStatus & PrintQueueFlags::PendingDeletion )
159 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PENDING );
160 if ( nStatus & PrintQueueFlags::Busy )
161 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_BUSY );
162 if ( nStatus & PrintQueueFlags::Initializing )
163 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_INITIALIZING );
164 if ( nStatus & PrintQueueFlags::Waiting )
165 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WAITING );
166 if ( nStatus & PrintQueueFlags::WarmingUp )
167 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_WARMING_UP );
168 if ( nStatus & PrintQueueFlags::Processing )
169 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PROCESSING );
170 if ( nStatus & PrintQueueFlags::Printing )
171 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PRINTING );
172 if ( nStatus & PrintQueueFlags::Offline )
173 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OFFLINE );
174 if ( nStatus & PrintQueueFlags::Error )
175 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_ERROR );
176 if ( nStatus & PrintQueueFlags::StatusUnknown )
177 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_SERVER_UNKNOWN );
178 if ( nStatus & PrintQueueFlags::PaperJam )
179 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_JAM );
180 if ( nStatus & PrintQueueFlags::PaperOut )
181 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_OUT );
182 if ( nStatus & PrintQueueFlags::ManualFeed )
183 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_MANUAL_FEED );
184 if ( nStatus & PrintQueueFlags::PaperProblem )
185 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAPER_PROBLEM );
186 if ( nStatus & PrintQueueFlags::IOActive )
187 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_IO_ACTIVE );
188 if ( nStatus & PrintQueueFlags::OutputBinFull )
189 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUTPUT_BIN_FULL );
190 if ( nStatus & PrintQueueFlags::TonerLow )
191 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_TONER_LOW );
192 if ( nStatus & PrintQueueFlags::NoToner )
193 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_NO_TONER );
194 if ( nStatus & PrintQueueFlags::PagePunt )
195 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_PAGE_PUNT );
196 if ( nStatus & PrintQueueFlags::UserIntervention )
197 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_USER_INTERVENTION );
198 if ( nStatus & PrintQueueFlags::OutOfMemory )
199 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_OUT_OF_MEMORY );
200 if ( nStatus & PrintQueueFlags::DoorOpen )
201 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DOOR_OPEN );
202 if ( nStatus & PrintQueueFlags::PowerSave )
203 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_POWER_SAVE );
205 // Number of jobs
206 sal_uInt32 nJobs = rInfo.GetJobs();
207 if ( nJobs && (nJobs != QUEUE_JOBS_DONTKNOW) )
209 OUString aJobStr( SvtResId( STR_SVT_PRNDLG_JOBCOUNT ) );
210 OUString aJobs( OUString::number( nJobs ) );
211 aStr = ImplPrnDlgAddString(aStr, aJobStr.replaceAll("%d", aJobs));
214 return aStr;
217 PrinterSetupDialog::PrinterSetupDialog(weld::Window* pParent)
218 : GenericDialogController(pParent, u"svt/ui/printersetupdialog.ui"_ustr, u"PrinterSetupDialog"_ustr)
219 , m_xLbName(m_xBuilder->weld_combo_box(u"name"_ustr))
220 , m_xBtnProperties(m_xBuilder->weld_button(u"properties"_ustr))
221 , m_xBtnOptions(m_xBuilder->weld_button(u"options"_ustr))
222 , m_xFiStatus(m_xBuilder->weld_label(u"status"_ustr))
223 , m_xFiType(m_xBuilder->weld_label(u"type"_ustr))
224 , m_xFiLocation(m_xBuilder->weld_label(u"location"_ustr))
225 , m_xFiComment(m_xBuilder->weld_label(u"comment"_ustr))
226 , maStatusTimer("PrinterSetupDialog maStatusTimer")
228 m_xLbName->make_sorted();
230 // show options button only if link is set
231 m_xBtnOptions->hide();
233 mpPrinter = nullptr;
234 mpTempPrinter = nullptr;
236 maStatusTimer.SetTimeout( IMPL_PRINTDLG_STATUS_UPDATE );
237 maStatusTimer.SetInvokeHandler( LINK( this, PrinterSetupDialog, ImplStatusHdl ) );
238 m_xBtnProperties->connect_clicked( LINK( this, PrinterSetupDialog, ImplPropertiesHdl ) );
239 m_xLbName->connect_changed( LINK( this, PrinterSetupDialog, ImplChangePrinterHdl ) );
240 m_xDialog->connect_focus_in( LINK( this, PrinterSetupDialog, ImplGetFocusHdl ) );
241 Application::AddEventListener(LINK( this, PrinterSetupDialog, ImplDataChangedHdl ) );
244 PrinterSetupDialog::~PrinterSetupDialog()
246 Application::RemoveEventListener(LINK( this, PrinterSetupDialog, ImplDataChangedHdl ) );
247 ImplFreePrnDlgListBox(m_xLbName.get(), false);
250 void PrinterSetupDialog::SetOptionsHdl(const Link<weld::Button&, void>& rLink)
252 m_xBtnOptions->connect_clicked(rLink);
253 m_xBtnOptions->set_accessible_description(SvtResId(STR_A11Y_DESC_OPTIONS));
254 m_xBtnOptions->set_visible(rLink.IsSet());
257 void PrinterSetupDialog::ImplSetInfo()
259 const QueueInfo* pInfo = Printer::GetQueueInfo(m_xLbName->get_active_text(), true);
260 if ( pInfo )
262 m_xFiType->set_label( pInfo->GetDriver() );
263 m_xFiLocation->set_label( pInfo->GetLocation() );
264 m_xFiComment->set_label( pInfo->GetComment() );
265 m_xFiStatus->set_label( ImplPrnDlgGetStatusText( *pInfo ) );
267 else
269 const OUString aTempStr;
270 m_xFiType->set_label( aTempStr );
271 m_xFiLocation->set_label( aTempStr );
272 m_xFiComment->set_label( aTempStr );
273 m_xFiStatus->set_label( aTempStr );
277 IMPL_LINK_NOARG(PrinterSetupDialog, ImplStatusHdl, Timer *, void)
279 QueueInfo aInfo;
280 ImplPrnDlgUpdateQueueInfo(m_xLbName.get(), aInfo);
281 m_xFiStatus->set_label( ImplPrnDlgGetStatusText( aInfo ) );
285 IMPL_LINK_NOARG(PrinterSetupDialog, ImplPropertiesHdl, weld::Button&, void)
287 if ( !mpTempPrinter )
288 mpTempPrinter = VclPtr<Printer>::Create( mpPrinter->GetJobSetup() );
289 mpTempPrinter->Setup(m_xDialog.get());
292 IMPL_LINK_NOARG(PrinterSetupDialog, ImplChangePrinterHdl, weld::ComboBox&, void)
294 mpTempPrinter = ImplPrnDlgListBoxSelect(m_xLbName.get(), m_xBtnProperties.get(),
295 mpPrinter, mpTempPrinter);
296 ImplSetInfo();
299 IMPL_LINK(PrinterSetupDialog, ImplGetFocusHdl, weld::Widget&, rWidget, void)
301 if (rWidget.is_visible())
302 ImplStatusHdl(&maStatusTimer);
305 IMPL_LINK(PrinterSetupDialog, ImplDataChangedHdl, VclSimpleEvent&, rEvt, void)
307 VclEventId nEvent = rEvt.GetId();
308 if (nEvent != VclEventId::ApplicationDataChanged)
309 return;
311 DataChangedEvent* pData = static_cast<DataChangedEvent*>(static_cast<VclWindowEvent&>(rEvt).GetData());
312 if (!pData || pData->GetType() != DataChangedEventType::PRINTER)
313 return;
315 mpTempPrinter = ImplPrnDlgUpdatePrinter(mpPrinter, mpTempPrinter);
316 Printer* pPrn;
317 if (mpTempPrinter)
318 pPrn = mpTempPrinter;
319 else
320 pPrn = mpPrinter;
321 ImplFillPrnDlgListBox(pPrn, m_xLbName.get(), m_xBtnProperties.get());
322 ImplSetInfo();
325 short PrinterSetupDialog::run()
327 if ( !mpPrinter || mpPrinter->IsPrinting() || mpPrinter->IsJobActive() )
329 SAL_WARN( "svtools.dialogs", "PrinterSetupDialog::execute() - No Printer or printer is printing" );
330 return RET_CANCEL;
333 Printer::updatePrinters();
335 ImplFillPrnDlgListBox(mpPrinter, m_xLbName.get(), m_xBtnProperties.get());
336 ImplSetInfo();
337 maStatusTimer.Start();
339 // start dialog
340 short nRet = GenericDialogController::run();
342 // update data if the dialog was terminated with OK
343 if ( nRet == RET_OK && mpTempPrinter )
344 mpPrinter->SetPrinterProps( mpTempPrinter );
346 maStatusTimer.Stop();
348 return nRet;
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */