LibreOffice Module svtools (master) 1
prnsetup.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
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>
32
33void ImplFillPrnDlgListBox( const Printer* pPrinter,
34 weld::ComboBox* pBox, weld::Button* pPropBtn )
35{
37
38 const std::vector<OUString>& rPrinters = Printer::GetPrinterQueues();
39 unsigned int nCount = rPrinters.size();
40 if ( nCount )
41 {
42 for( unsigned int i = 0; i < nCount; i++ )
43 pBox->append_text( rPrinters[i] );
44 pBox->set_active_text(pPrinter->GetName());
45 }
46
47 pBox->set_sensitive(nCount != 0);
48 pPropBtn->set_visible( pPrinter->HasSupport( PrinterSupport::SetupDialog ) );
49}
50
51
52void ImplFreePrnDlgListBox( weld::ComboBox* pBox, bool bClear )
53{
54 if ( bClear )
55 pBox->clear();
56}
57
58
60 Printer const * pPrinter, Printer* pTempPrinterIn )
61{
62 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
63 if ( pBox->get_active() != -1 )
64 {
65 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
66 if( pInfo)
67 {
68 if ( !pTempPrinter )
69 {
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 );
75 }
76 else
77 {
78 if ( (pTempPrinter->GetName() != pInfo->GetPrinterName()) ||
79 (pTempPrinter->GetDriverName() != pInfo->GetDriver()) )
80 {
81 pTempPrinter.disposeAndClear();
82 pTempPrinter = VclPtr<Printer>::Create( *pInfo );
83 }
84 }
85
86 pPropBtn->set_sensitive(pTempPrinter->HasSupport(PrinterSupport::SetupDialog));
87 }
88 else
89 pPropBtn->set_sensitive(false);
90 }
91 else
92 pPropBtn->set_sensitive(false);
93
94 return pTempPrinter;
95}
96
97
98Printer* ImplPrnDlgUpdatePrinter( Printer const * pPrinter, Printer* pTempPrinterIn )
99{
100 VclPtr<Printer> pTempPrinter( pTempPrinterIn );
101 OUString aPrnName;
102 if ( pTempPrinter )
103 aPrnName = pTempPrinter->GetName();
104 else
105 aPrnName = pPrinter->GetName();
106
107 if ( ! Printer::GetQueueInfo( aPrnName, false ) )
108 {
109 pTempPrinter.disposeAndClear();
110 pTempPrinter = VclPtr<Printer>::Create();
111 }
112
113 return pTempPrinter;
114}
115
116
118{
119 if ( pBox->get_active() != -1 )
120 {
121 const QueueInfo* pInfo = Printer::GetQueueInfo( pBox->get_active_text(), true );
122 if( pInfo )
123 rInfo = *pInfo;
124 }
125}
126
127
128static OUString ImplPrnDlgAddString(const OUString& rStr, std::u16string_view rAddStr)
129{
130 OUString aStr(rStr);
131 if (!aStr.isEmpty())
132 aStr += "; " ;
133 return aStr + rAddStr;
134}
135
136
137static OUString ImplPrnDlgAddResString(const OUString& rStr, TranslateId pResId)
138{
139 return ImplPrnDlgAddString(rStr, SvtResId(pResId));
140}
141
142
143OUString ImplPrnDlgGetStatusText( const QueueInfo& rInfo )
144{
145 OUString aStr;
146 PrintQueueFlags nStatus = rInfo.GetStatus();
147
148 // Default-Printer
149 if ( !rInfo.GetPrinterName().isEmpty() &&
151 aStr = ImplPrnDlgAddResString( aStr, STR_SVT_PRNDLG_DEFPRINTER );
152
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 );
204
205 // Number of jobs
206 sal_uInt32 nJobs = rInfo.GetJobs();
207 if ( nJobs && (nJobs != QUEUE_JOBS_DONTKNOW) )
208 {
209 OUString aJobStr( SvtResId( STR_SVT_PRNDLG_JOBCOUNT ) );
210 OUString aJobs( OUString::number( nJobs ) );
211 aStr = ImplPrnDlgAddString(aStr, aJobStr.replaceAll("%d", aJobs));
212 }
213
214 return aStr;
215}
216
218 : GenericDialogController(pParent, "svt/ui/printersetupdialog.ui", "PrinterSetupDialog")
219 , m_xLbName(m_xBuilder->weld_combo_box("name"))
220 , m_xBtnProperties(m_xBuilder->weld_button("properties"))
221 , m_xBtnOptions(m_xBuilder->weld_button("options"))
222 , m_xFiStatus(m_xBuilder->weld_label("status"))
223 , m_xFiType(m_xBuilder->weld_label("type"))
224 , m_xFiLocation(m_xBuilder->weld_label("location"))
225 , m_xFiComment(m_xBuilder->weld_label("comment"))
226 , maStatusTimer("PrinterSetupDialog maStatusTimer")
227{
228 m_xLbName->make_sorted();
229
230 // show options button only if link is set
231 m_xBtnOptions->hide();
232
233 mpPrinter = nullptr;
234 mpTempPrinter = nullptr;
235
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 ) );
242}
243
245{
246 Application::RemoveEventListener(LINK( this, PrinterSetupDialog, ImplDataChangedHdl ) );
247 ImplFreePrnDlgListBox(m_xLbName.get(), false);
248}
249
251{
252 m_xBtnOptions->connect_clicked(rLink);
253 m_xBtnOptions->set_accessible_description(SvtResId(STR_A11Y_DESC_OPTIONS));
254 m_xBtnOptions->set_visible(rLink.IsSet());
255}
256
258{
259 const QueueInfo* pInfo = Printer::GetQueueInfo(m_xLbName->get_active_text(), true);
260 if ( pInfo )
261 {
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 ) );
266 }
267 else
268 {
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 );
274 }
275}
276
278{
279 QueueInfo aInfo;
280 ImplPrnDlgUpdateQueueInfo(m_xLbName.get(), aInfo);
281 m_xFiStatus->set_label( ImplPrnDlgGetStatusText( aInfo ) );
282}
283
284
286{
287 if ( !mpTempPrinter )
288 mpTempPrinter = VclPtr<Printer>::Create( mpPrinter->GetJobSetup() );
289 mpTempPrinter->Setup(m_xDialog.get());
290}
291
293{
294 mpTempPrinter = ImplPrnDlgListBoxSelect(m_xLbName.get(), m_xBtnProperties.get(),
295 mpPrinter, mpTempPrinter);
296 ImplSetInfo();
297}
298
299IMPL_LINK(PrinterSetupDialog, ImplGetFocusHdl, weld::Widget&, rWidget, void)
300{
301 if (rWidget.is_visible())
302 ImplStatusHdl(&maStatusTimer);
303}
304
305IMPL_LINK(PrinterSetupDialog, ImplDataChangedHdl, VclSimpleEvent&, rEvt, void)
306{
307 VclEventId nEvent = rEvt.GetId();
308 if (nEvent != VclEventId::ApplicationDataChanged)
309 return;
310
311 DataChangedEvent* pData = static_cast<DataChangedEvent*>(static_cast<VclWindowEvent&>(rEvt).GetData());
312 if (!pData || pData->GetType() != DataChangedEventType::PRINTER)
313 return;
314
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();
323}
324
326{
327 if ( !mpPrinter || mpPrinter->IsPrinting() || mpPrinter->IsJobActive() )
328 {
329 SAL_WARN( "svtools.dialogs", "PrinterSetupDialog::execute() - No Printer or printer is printing" );
330 return RET_CANCEL;
331 }
332
334
336 ImplSetInfo();
338
339 // start dialog
340 short nRet = GenericDialogController::run();
341
342 // update data if the dialog was terminated with OK
343 if ( nRet == RET_OK && mpTempPrinter )
344 mpPrinter->SetPrinterProps( mpTempPrinter );
345
347
348 return nRet;
349}
350
351/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
static void AddEventListener(const Link< VclSimpleEvent &, void > &rEventListener)
static void RemoveEventListener(const Link< VclSimpleEvent &, void > &rEventListener)
std::unique_ptr< weld::Label > m_xFiType
Definition: prnsetup.hxx:38
std::unique_ptr< weld::Label > m_xFiComment
Definition: prnsetup.hxx:40
virtual short run() override
Definition: prnsetup.cxx:325
std::unique_ptr< weld::Button > m_xBtnOptions
Definition: prnsetup.hxx:36
std::unique_ptr< weld::Label > m_xFiLocation
Definition: prnsetup.hxx:39
VclPtr< Printer > mpPrinter
Definition: prnsetup.hxx:42
std::unique_ptr< weld::ComboBox > m_xLbName
Definition: prnsetup.hxx:34
PrinterSetupDialog(weld::Window *pWindow)
Definition: prnsetup.cxx:217
SVT_DLLPRIVATE void ImplSetInfo()
Definition: prnsetup.cxx:257
VclPtr< Printer > mpTempPrinter
Definition: prnsetup.hxx:43
std::unique_ptr< weld::Button > m_xBtnProperties
Definition: prnsetup.hxx:35
void SetOptionsHdl(const Link< weld::Button &, void > &rLink)
Definition: prnsetup.cxx:250
AutoTimer maStatusTimer
Definition: prnsetup.hxx:41
std::unique_ptr< weld::Label > m_xFiStatus
Definition: prnsetup.hxx:37
virtual ~PrinterSetupDialog() override
Definition: prnsetup.cxx:244
static OUString GetDefaultPrinterName()
bool HasSupport(PrinterSupport eFeature) const
const OUString & GetDriverName() const
static void updatePrinters()
static const std::vector< OUString > & GetPrinterQueues()
static const QueueInfo * GetQueueInfo(const OUString &rPrinterName, bool bStatusUpdate)
const OUString & GetName() const
const JobSetup & GetJobSetup() const
const OUString & GetComment() const
PrintQueueFlags GetStatus() const
const OUString & GetDriver() const
sal_uInt32 GetJobs() const
const OUString & GetLocation() const
const OUString & GetPrinterName() const
void Stop()
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
void disposeAndClear()
static VclPtr< reference_type > Create(Arg &&... arg)
virtual OUString get_active_text() const=0
virtual void clear()=0
void append_text(const OUString &rStr)
virtual int get_active() const=0
void set_active_text(const OUString &rStr)
std::shared_ptr< weld::Dialog > m_xDialog
virtual void set_sensitive(bool sensitive)=0
virtual void set_visible(bool visible)
int nCount
#define SAL_WARN(area, stream)
aStr
std::unique_ptr< sal_Int32[]> pData
int i
Printer * ImplPrnDlgListBoxSelect(const weld::ComboBox *pBox, weld::Button *pPropBtn, Printer const *pPrinter, Printer *pTempPrinterIn)
Definition: prnsetup.cxx:59
IMPL_LINK_NOARG(PrinterSetupDialog, ImplStatusHdl, Timer *, void)
Definition: prnsetup.cxx:277
static OUString ImplPrnDlgAddResString(const OUString &rStr, TranslateId pResId)
Definition: prnsetup.cxx:137
IMPL_LINK(PrinterSetupDialog, ImplGetFocusHdl, weld::Widget &, rWidget, void)
Definition: prnsetup.cxx:299
OUString ImplPrnDlgGetStatusText(const QueueInfo &rInfo)
Definition: prnsetup.cxx:143
void ImplFillPrnDlgListBox(const Printer *pPrinter, weld::ComboBox *pBox, weld::Button *pPropBtn)
Definition: prnsetup.cxx:33
Printer * ImplPrnDlgUpdatePrinter(Printer const *pPrinter, Printer *pTempPrinterIn)
Definition: prnsetup.cxx:98
static OUString ImplPrnDlgAddString(const OUString &rStr, std::u16string_view rAddStr)
Definition: prnsetup.cxx:128
void ImplPrnDlgUpdateQueueInfo(const weld::ComboBox *pBox, QueueInfo &rInfo)
Definition: prnsetup.cxx:117
void ImplFreePrnDlgListBox(weld::ComboBox *pBox, bool bClear)
Definition: prnsetup.cxx:52
#define IMPL_PRINTDLG_STATUS_UPDATE
Definition: prnsetup.hxx:68
constexpr sal_uInt32 QUEUE_JOBS_DONTKNOW
PrintQueueFlags
OUString SvtResId(TranslateId aId)
Definition: svtresid.cxx:24
RET_OK
RET_CANCEL
VclEventId