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 
30 #include <com/sun/star/frame/XDesktop2.hpp>
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>
44 #include <unotools/configmgr.hxx>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 
47 //size of preview
48 const Size ThumbSize(150, 150);
49 
51  : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
52  , m_pParent(pParent)
53  , m_pText(m_xBuilder->weld_label("lbText"))
54  , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
55  , m_pNext(m_xBuilder->weld_button("btnNext"))
56  , m_pLink(m_xBuilder->weld_link_button("btnLink"))
57  , m_pPreview(new weld::CustomWeld(*m_xBuilder, "imPreview", m_aPreview))
58 {
59  m_pShowTip->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
60  m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
61  m_nCurrentTip = officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
62  m_pPreview->set_size_request(ThumbSize.Width(), ThumbSize.Height());
63 
64  if (pParent != nullptr)
65  {
66  css::uno::Reference<css::awt::XWindow> xWindow = pParent->GetXWindow();
67  if (xWindow.is())
68  {
70  if (xVclWin != nullptr)
71  xVclWin->AddEventListener(LINK(this, TipOfTheDayDialog, Terminated));
72  }
73  }
74 
75  const auto t0 = std::chrono::system_clock::now().time_since_epoch();
76  m_nDay = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24;
77  if (m_nDay > officecfg::Office::Common::Misc::LastTipOfTheDayShown::get())
78  m_nCurrentTip++;
79 
80  UpdateTip();
81 }
82 
83 IMPL_LINK(TipOfTheDayDialog, Terminated, VclWindowEvent&, rEvent, void)
84 {
85  if (rEvent.GetId() == VclEventId::ObjectDying)
87 }
88 
90 {
91  std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
93  officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(m_nDay, xChanges);
94  officecfg::Office::Common::Misc::LastTipOfTheDayID::set(m_nCurrentTip, xChanges);
95  officecfg::Office::Common::Misc::ShowTipOfTheDay::set(m_pShowTip->get_active(), xChanges);
96  xChanges->commit();
97 
98  if (m_pParent != nullptr)
99  {
100  css::uno::Reference<css::awt::XWindow> xWindow = m_pParent->GetXWindow();
101  if (xWindow.is())
102  {
104  if (xVclWin != nullptr)
105  xVclWin->RemoveEventListener(LINK(this, TipOfTheDayDialog, Terminated));
106  }
107  }
108 }
109 
110 static bool file_exists(const OUString& fileName)
111 {
112  ::osl::File aFile(fileName);
113  return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
114 }
115 
117 {
118  constexpr sal_Int32 nNumberOfTips = SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY);
119 
120  if ((m_nCurrentTip >= nNumberOfTips) || (m_nCurrentTip < 0))
121  m_nCurrentTip = 0;
122 
123  //title
124  m_xDialog->set_title(CuiResId(STR_TITLE)
125  .replaceFirst("%CURRENT", OUString::number(m_nCurrentTip + 1))
126  .replaceFirst("%TOTAL", OUString::number(nNumberOfTips)));
127 
128  auto[sTip, sLink, sImage] = TIPOFTHEDAY_STRINGARRAY[m_nCurrentTip];
129 
130  // text
131 //replace MOD1 & MOD2 shortcuts depending on platform
132 #ifdef MACOSX
133  const OUString aMOD1 = CuiResId(STR_CMD);
134  const OUString aMOD2 = CuiResId(STR_Option);
135 #else
136  const OUString aMOD1 = CuiResId(STR_CTRL);
137  const OUString aMOD2 = CuiResId(STR_Alt);
138 #endif
139  m_pText->set_label(CuiResId(sTip).replaceAll("%MOD1", aMOD1).replaceAll("%MOD2", aMOD2));
140 
141  // hyperlink
142  if (sLink.isEmpty())
143  {
144  m_pLink->set_visible(false);
145  }
146  else if (sLink.startsWith(".uno:"))
147  {
148  m_pLink->set_visible(false);
149  //show the link only if the UNO command is available in the current module
150  SfxViewFrame* pViewFrame = SfxViewFrame::Current();
151  if (pViewFrame)
152  {
153  const auto xFrame = pViewFrame->GetFrame().GetFrameInterface();
154  const css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(
155  xFrame, css::uno::UNO_QUERY);
156  if (xDispatchProvider.is())
157  {
158  css::util::URL aCommandURL;
159  aCommandURL.Complete = sLink;
160  const css::uno::Reference<css::uno::XComponentContext> xContext
162  const css::uno::Reference<css::util::XURLTransformer> xParser
163  = css::util::URLTransformer::create(xContext);
164  xParser->parseStrict(aCommandURL);
165  const css::uno::Reference<css::frame::XDispatch> xDisp
166  = xDispatchProvider->queryDispatch(aCommandURL, OUString(), 0);
167  if (xDisp.is())
168  {
169  m_pLink->set_label(CuiResId(STR_UNO_LINK));
170  m_pLink->set_uri(sLink);
171 
172  const OUString aModuleName(
174  const auto aProperties
176  m_pLink->set_tooltip_text(
178 
179  m_pLink->set_visible(true);
180  m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
181  }
182  }
183  }
184  }
185  else if (sLink.startsWith("http"))
186  {
187  // Links may have some %PRODUCTVERSION which need to be expanded
188  OUString aText = Translate::ExpandVariables(sLink);
190  if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
192  m_pLink->set_uri(aText.replaceFirst("%LANGUAGENAME", aLang));
193  m_pLink->set_label(CuiResId(STR_MORE_LINK));
194  m_pLink->set_visible(true);
195  m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
196  }
197  else
198  {
199  m_pLink->set_uri(sLink);
200  m_pLink->set_label(CuiResId(STR_HELP_LINK));
201  m_pLink->set_visible(true);
202  m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
203  }
204  // image
205  OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
206  rtl::Bootstrap::expandMacros(aURL);
207  OUString aImageName = sImage;
208  // use default image if none is available with the number
209  if (aImageName.isEmpty() || !file_exists(aURL + aImageName))
210  aImageName = "tipoftheday.png";
211  Graphic aGraphic;
212  GraphicFilter::LoadGraphic(aURL + aImageName, OUString(), aGraphic);
213 
214  if (!aGraphic.IsAnimated())
215  {
216  BitmapEx aBmpEx(aGraphic.GetBitmapEx());
217  if (aBmpEx.Scale(ThumbSize))
218  aGraphic = aBmpEx;
219  }
220  m_aPreview.SetPreview(aGraphic);
221 }
222 
223 IMPL_LINK(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, rButton, bool)
224 {
225  const OUString sLink = rButton.get_uri();
226  if (sLink.startsWith(".uno:"))
227  {
228  comphelper::dispatchCommand(sLink, {});
230  }
231  else
232  {
233  Application::GetHelp()->Start(sLink, static_cast<weld::Widget*>(nullptr));
234  }
235  return true;
236 }
237 
239 {
240  m_nCurrentTip++; //zeroed at updatetip when out of range
241  UpdateTip();
242 }
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
const size_t count(pCandidateA->getBorderLines().size())
URL aURL
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
const Size ThumbSize(150, 150)
OUString ExpandVariables(const OUString &rString)
static Help * GetHelp()
Reference< XFrame > xFrame
virtual bool Start(const OUString &rHelpId, weld::Widget *pWidget=nullptr)
const OUString & getBcp47(bool bResolveSystem=true) const
std::unique_ptr< weld::Label > m_pText
OUString CuiResId(std::string_view aKey)
Definition: cuiresmgr.cxx:23
std::unique_ptr< weld::CustomWeld > m_pPreview
constexpr tools::Long Width() const
CuiGraphicPreviewWindow m_aPreview
PropertiesInfo aProperties
OUString getLanguage() const
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)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
SfxFrame & GetFrame() const
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
std::unique_ptr< weld::LinkButton > m_pLink
IMPL_LINK_NOARG(TipOfTheDayDialog, OnNextClick, weld::Button &, void)
bool IsAnimated() const
#define SAL_N_ELEMENTS(arr)
static ErrCode LoadGraphic(const OUString &rPath, const OUString &rFilter, Graphic &rGraphic, GraphicFilter *pFilter=nullptr, sal_uInt16 *pDeterminedFormat=nullptr)
std::unique_ptr< weld::CheckButton > m_pShowTip
OUString GetModuleIdentifier(const Reference< frame::XFrame > &rxFrame)
static bool file_exists(const OUString &fileName)
TipOfTheDayDialog(weld::Window *pWindow)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
static OUString getUILocale()
void SetPreview(const Graphic &rGraphic)
Definition: cuigrfflt.cxx:71
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
void response(int nResponse)
virtual ~TipOfTheDayDialog() override
Sequence< beans::PropertyValue > GetCommandProperties(const OUString &rsCommandName, const OUString &rsModuleName)
void AddEventListener(const Link< VclWindowEvent &, void > &rEventListener)
static SfxViewFrame * Current()
constexpr tools::Long Height() const
std::unique_ptr< weld::Button > m_pNext
RET_OK
weld::Window * m_pParent
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK(TipOfTheDayDialog, Terminated, VclWindowEvent &, rEvent, void)
void RemoveEventListener(const Link< VclWindowEvent &, void > &rEventListener)
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)