LibreOffice Module cui (master) 1
tipofthedaydlg.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#include <tipofthedaydlg.hxx>
22#include <tipoftheday.hrc>
23
24#include <sfx2/viewfrm.hxx>
26#include <vcl/graphicfilter.hxx>
27#include <vcl/help.hxx>
28#include <vcl/window.hxx>
29#include <vcl/ImageTree.hxx>
30
31#include <com/sun/star/frame/XDispatch.hpp>
32#include <com/sun/star/frame/XDispatchProvider.hpp>
33#include <com/sun/star/util/URL.hpp>
34#include <com/sun/star/util/URLTransformer.hpp>
35
37#include <dialmgr.hxx>
39#include <officecfg/Office/Common.hxx>
40#include <osl/file.hxx>
41#include <rtl/bootstrap.hxx>
43#include <unotools/resmgr.hxx>
45#include <com/sun/star/beans/PropertyValue.hpp>
46#include <bitmaps.hlst>
47
48//size of preview
49const Size ThumbSize(150, 150);
50
52 : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
53 , m_pParent(pParent)
54 , m_pText(m_xBuilder->weld_label("lbText"))
55 , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
56 , m_pNext(m_xBuilder->weld_button("btnNext"))
57 , m_pLink(m_xBuilder->weld_link_button("btnLink"))
58 , m_pPreview(new weld::CustomWeld(*m_xBuilder, "imPreview", m_aPreview))
59{
60 m_pShowTip->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
61 m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
62 m_nCurrentTip = officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
63 m_pPreview->set_size_request(ThumbSize.Width(), ThumbSize.Height());
64
65 if (pParent != nullptr)
66 {
67 css::uno::Reference<css::awt::XWindow> xWindow = pParent->GetXWindow();
68 if (xWindow.is())
69 {
71 if (xVclWin != nullptr)
72 xVclWin->AddEventListener(LINK(this, TipOfTheDayDialog, Terminated));
73 }
74 }
75
76 const auto t0 = std::chrono::system_clock::now().time_since_epoch();
77 sal_Int32 nDay = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24;
78 //show next tip after one day
79 if (nDay > officecfg::Office::Common::Misc::LastTipOfTheDayShown::get())
81
82 // save this time to the config now instead of in the dtor otherwise we
83 // end up with multiple copies of this dialog every time we open a new
84 // document if the first one isn't closed
85 std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
87 officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(nDay, xChanges);
88 xChanges->commit();
89
90 UpdateTip();
91}
92
93IMPL_LINK(TipOfTheDayDialog, Terminated, VclWindowEvent&, rEvent, void)
94{
95 if (rEvent.GetId() == VclEventId::ObjectDying)
96 {
97 m_pParent = nullptr;
99 }
100}
101
103{
104 std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
106 officecfg::Office::Common::Misc::LastTipOfTheDayID::set(m_nCurrentTip, xChanges);
107 officecfg::Office::Common::Misc::ShowTipOfTheDay::set(m_pShowTip->get_active(), xChanges);
108 xChanges->commit();
109
110 if (m_pParent != nullptr)
111 {
112 css::uno::Reference<css::awt::XWindow> xWindow = m_pParent->GetXWindow();
113 if (xWindow.is())
114 {
116 if (xVclWin != nullptr)
117 xVclWin->RemoveEventListener(LINK(this, TipOfTheDayDialog, Terminated));
118 }
119 }
120}
121
122static bool file_exists(const OUString& fileName)
123{
124 ::osl::File aFile(fileName);
125 return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
126}
127
129{
130 constexpr sal_Int32 nNumberOfTips = std::size(TIPOFTHEDAY_STRINGARRAY);
131
132 if ((m_nCurrentTip >= nNumberOfTips) || (m_nCurrentTip < 0))
133 m_nCurrentTip = 0;
134
135 //title
136 m_xDialog->set_title(CuiResId(STR_TITLE)
137 .replaceFirst("%CURRENT", OUString::number(m_nCurrentTip + 1))
138 .replaceFirst("%TOTAL", OUString::number(nNumberOfTips)));
139
140 auto[sTip, sLink, sImage, nType] = TIPOFTHEDAY_STRINGARRAY[m_nCurrentTip];
141
142 // text
143//replace MOD1 & MOD2 shortcuts depending on platform
144#ifdef MACOSX
145 const OUString aMOD1 = CuiResId(STR_CMD);
146 const OUString aMOD2 = CuiResId(STR_Option);
147#else
148 const OUString aMOD1 = CuiResId(STR_CTRL);
149 const OUString aMOD2 = CuiResId(STR_Alt);
150#endif
151 m_pText->set_label(CuiResId(sTip).replaceAll("%MOD1", aMOD1).replaceAll("%MOD2", aMOD2));
152
153 // hyperlink
154 if (sLink.isEmpty())
155 {
156 m_pLink->set_visible(false);
157 }
158 else if (sLink.startsWith(".uno:"))
159 {
160 m_pLink->set_visible(false);
161 //show the link only if the UNO command is available in the current module
162 if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
163 {
164 const auto xFrame = pViewFrame->GetFrame().GetFrameInterface();
165 const css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(
166 xFrame, css::uno::UNO_QUERY);
167 if (xDispatchProvider.is())
168 {
169 css::util::URL aCommandURL;
170 aCommandURL.Complete = sLink;
171 const css::uno::Reference<css::uno::XComponentContext> xContext
173 const css::uno::Reference<css::util::XURLTransformer> xParser
174 = css::util::URLTransformer::create(xContext);
175 xParser->parseStrict(aCommandURL);
176 const css::uno::Reference<css::frame::XDispatch> xDisp
177 = xDispatchProvider->queryDispatch(aCommandURL, OUString(), 0);
178 if (xDisp.is())
179 {
180 m_pLink->set_label(CuiResId(STR_UNO_LINK));
181 m_pLink->set_uri(sLink);
182
183 const OUString aModuleName(
185 const auto aProperties
187 m_pLink->set_tooltip_text(
189
190 m_pLink->set_visible(true);
191 m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
192 }
193 }
194 }
195 }
196 else if (sLink.startsWith("http"))
197 {
198 // Links may have some %PRODUCTVERSION which need to be expanded
199 OUString aText = Translate::ExpandVariables(sLink);
201 if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
203 m_pLink->set_uri(aText.replaceFirst("%LANGUAGENAME", aLang));
204 m_pLink->set_label(CuiResId(STR_MORE_LINK));
205 m_pLink->set_visible(true);
206 m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
207 }
208 else
209 {
210 m_pLink->set_uri(sLink);
211 m_pLink->set_label(CuiResId(STR_HELP_LINK));
212 m_pLink->set_visible(true);
213 m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
214 }
215 // image
216 OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
217 rtl::Bootstrap::expandMacros(aURL);
218 OUString aImageName = sImage;
219 Graphic aGraphic;
220
221 if (!aImageName.isEmpty() && file_exists(aURL + aImageName))
222 GraphicFilter::LoadGraphic(aURL + aImageName, OUString(), aGraphic);
223 else
224 {
225 const OUString sModuleImage[5]
226 = { RID_SVXBMP_TOTD_WRITER, RID_SVXBMP_TOTD_CALC, RID_SVXBMP_TOTD_DRAW,
227 RID_SVXBMP_TOTD_IMPRESS, RID_SVXBMP_TOTD_SOFFICE };
228 const OUString aIconTheme
230 BitmapEx aBmpEx;
231 ImageTree::get().loadImage(sModuleImage[nType], aIconTheme, aBmpEx, true,
232 ImageLoadFlags::IgnoreDarkTheme);
233 aGraphic = aBmpEx;
234 }
235
236 if (!aGraphic.IsAnimated())
237 {
238 BitmapEx aBmpEx(aGraphic.GetBitmapEx());
239 if (aBmpEx.Scale(ThumbSize))
240 aGraphic = aBmpEx;
241 }
242 m_aPreview.SetPreview(aGraphic);
243}
244
245IMPL_LINK(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, rButton, bool)
246{
247 const OUString sLink = rButton.get_uri();
248 if (sLink.startsWith(".uno:"))
249 {
252 }
253 else
254 {
255 Application::GetHelp()->Start(sLink, static_cast<weld::Widget*>(nullptr));
256 }
257 return true;
258}
259
261{
262 m_nCurrentTip++; //zeroed at updatetip when out of range
263 UpdateTip();
264}
265
266/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
static Help * GetHelp()
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
void SetPreview(const Graphic &rGraphic)
Definition: cuigrfflt.cxx:71
static ErrCode LoadGraphic(const OUString &rPath, const OUString &rFilter, Graphic &rGraphic, GraphicFilter *pFilter=nullptr, sal_uInt16 *pDeterminedFormat=nullptr)
bool IsAnimated() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
virtual bool Start(const OUString &rHelpId, weld::Widget *pWidget=nullptr)
static VCL_DLLPUBLIC ImageTree & get()
VCL_DLLPUBLIC bool loadImage(OUString const &name, OUString const &style, BitmapEx &bitmap, bool localized, const ImageLoadFlags eFlags=ImageLoadFlags::NONE)
OUString getLanguage() const
const OUString & getBcp47(bool bResolveSystem=true) const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
constexpr tools::Long Height() const
constexpr tools::Long Width() const
OUString DetermineIconTheme() const
CuiGraphicPreviewWindow m_aPreview
std::unique_ptr< weld::Label > m_pText
weld::Window * m_pParent
TipOfTheDayDialog(weld::Window *pWindow)
std::unique_ptr< weld::LinkButton > m_pLink
std::unique_ptr< weld::CustomWeld > m_pPreview
std::unique_ptr< weld::Button > m_pNext
std::unique_ptr< weld::CheckButton > m_pShowTip
virtual ~TipOfTheDayDialog() override
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
static std::shared_ptr< ConfigurationChanges > create()
static OUString getUILocale()
void response(int nResponse)
std::shared_ptr< weld::Dialog > m_xDialog
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
OUString CuiResId(TranslateId aKey)
Definition: cuiresmgr.cxx:23
URL aURL
OUString ExpandVariables(const OUString &rString)
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
Reference< XComponentContext > getProcessComponentContext()
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
QPRO_FUNC_TYPE nType
Reference< XFrame > xFrame
IMPL_LINK_NOARG(TipOfTheDayDialog, OnNextClick, weld::Button &, void)
IMPL_LINK(TipOfTheDayDialog, Terminated, VclWindowEvent &, rEvent, void)
const Size ThumbSize(150, 150)
static bool file_exists(const OUString &fileName)
RET_OK