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 
29 #include <com/sun/star/frame/XDesktop2.hpp>
30 #include <com/sun/star/frame/XDispatch.hpp>
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
32 #include <com/sun/star/util/URL.hpp>
33 #include <com/sun/star/util/URLTransformer.hpp>
34 
36 #include <dialmgr.hxx>
38 #include <officecfg/Office/Common.hxx>
39 #include <osl/file.hxx>
40 #include <rtl/bootstrap.hxx>
41 #include <unotools/resmgr.hxx>
42 #include <unotools/configmgr.hxx>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 
46  : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
47  , m_pText(m_xBuilder->weld_label("lbText"))
48  , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
49  , m_pNext(m_xBuilder->weld_button("btnNext"))
50  , m_pLink(m_xBuilder->weld_link_button("btnLink"))
51  , m_pPreview(new weld::CustomWeld(*m_xBuilder, "imPreview", m_aPreview))
52 {
53  m_pShowTip->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
54  m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
55 
56  m_nCurrentTip = officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
57 
58  const auto t0 = std::chrono::system_clock::now().time_since_epoch();
59  m_nDay = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24;
60  if (m_nDay > officecfg::Office::Common::Misc::LastTipOfTheDayShown::get())
61  m_nCurrentTip++;
62 
63  UpdateTip();
64 }
65 
67 {
68  std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
70  officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(m_nDay, xChanges);
71  officecfg::Office::Common::Misc::LastTipOfTheDayID::set(m_nCurrentTip, xChanges);
72  officecfg::Office::Common::Misc::ShowTipOfTheDay::set(m_pShowTip->get_active(), xChanges);
73  xChanges->commit();
74 }
75 
76 static bool file_exists(const OUString& fileName)
77 {
78  ::osl::File aFile(fileName);
79  return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
80 }
81 
83 {
84  constexpr sal_Int32 nNumberOfTips = SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY);
85 
86  if ((m_nCurrentTip >= nNumberOfTips) || (m_nCurrentTip < 0))
87  m_nCurrentTip = 0;
88 
89  //title
90  m_xDialog->set_title(CuiResId(STR_TITLE)
91  .replaceFirst("%CURRENT", OUString::number(m_nCurrentTip + 1))
92  .replaceFirst("%TOTAL", OUString::number(nNumberOfTips)));
93 
94  auto[sTip, sLink, sImage] = TIPOFTHEDAY_STRINGARRAY[m_nCurrentTip];
95 
96  // text
97 //replace MOD1 & MOD2 shortcuts depending on platform
98 #ifdef MACOSX
99  const OUString aMOD1 = CuiResId(STR_CMD);
100  const OUString aMOD2 = CuiResId(STR_Option);
101 #else
102  const OUString aMOD1 = CuiResId(STR_CTRL);
103  const OUString aMOD2 = CuiResId(STR_Alt);
104 #endif
105  m_pText->set_label(CuiResId(sTip).replaceAll("%MOD1", aMOD1).replaceAll("%MOD2", aMOD2));
106 
107  // hyperlink
108  if (sLink.isEmpty())
109  {
110  m_pLink->set_visible(false);
111  }
112  else if (sLink.startsWith(".uno:"))
113  {
114  m_pLink->set_visible(false);
115  //show the link only if the UNO command is available in the current module
116  SfxViewFrame* pViewFrame = SfxViewFrame::Current();
117  if (pViewFrame)
118  {
119  const auto xFrame = pViewFrame->GetFrame().GetFrameInterface();
120  const css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(
121  xFrame, css::uno::UNO_QUERY);
122  if (xDispatchProvider.is())
123  {
124  css::util::URL aCommandURL;
125  aCommandURL.Complete = sLink;
126  const css::uno::Reference<css::uno::XComponentContext> xContext
128  const css::uno::Reference<css::util::XURLTransformer> xParser
129  = css::util::URLTransformer::create(xContext);
130  xParser->parseStrict(aCommandURL);
131  const css::uno::Reference<css::frame::XDispatch> xDisp
132  = xDispatchProvider->queryDispatch(aCommandURL, OUString(), 0);
133  if (xDisp.is())
134  {
135  m_pLink->set_label(CuiResId(STR_UNO_LINK));
136  m_pLink->set_uri(sLink);
137 
138  const OUString aModuleName(
140  const auto aProperties
142  m_pLink->set_tooltip_text(
144 
145  m_pLink->set_visible(true);
146  m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
147  }
148  }
149  }
150  }
151  else if (sLink.startsWith("http"))
152  {
153  // Links may have some %PRODUCTVERSION which need to be expanded
154  OUString aText = Translate::ExpandVariables(sLink);
156  if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
158  m_pLink->set_uri(aText.replaceFirst("%LANGUAGENAME", aLang));
159  m_pLink->set_label(CuiResId(STR_MORE_LINK));
160  m_pLink->set_visible(true);
161  m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
162  }
163  else
164  {
165  m_pLink->set_uri(sLink);
166  m_pLink->set_label(CuiResId(STR_HELP_LINK));
167  m_pLink->set_visible(true);
168  m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
169  }
170  // image
171  OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
172  rtl::Bootstrap::expandMacros(aURL);
173  OUString aImageName = sImage;
174  // use default image if none is available with the number
175  if (aImageName.isEmpty() || !file_exists(aURL + aImageName))
176  aImageName = "tipoftheday.png";
177  Graphic aGraphic;
178  GraphicFilter::LoadGraphic(aURL + aImageName, OUString(), aGraphic);
179  m_aPreview.SetPreview(aGraphic);
180 }
181 
182 IMPL_LINK(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, rButton, bool)
183 {
184  const OUString sLink = rButton.get_uri();
185  if (sLink.startsWith(".uno:"))
186  {
187  comphelper::dispatchCommand(sLink, {});
189  }
190  else
191  {
192  Application::GetHelp()->Start(sLink, static_cast<weld::Widget*>(nullptr));
193  }
194  return true;
195 }
196 
198 {
199  m_nCurrentTip++; //zeroed at updatetip when out of range
200  UpdateTip();
201 }
202 
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
const size_t count(pCandidateA->getBorderLines().size())
URL aURL
OUString ExpandVariables(const OUString &rString)
static Help * GetHelp()
Reference< XFrame > xFrame
const OUString & getBcp47(bool bResolveSystem=true) const
std::unique_ptr< weld::Label > m_pText
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)
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)
#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)
virtual bool Start(const OUString &rHelpId, const vcl::Window *pWindow)
TipOfTheDayDialog(weld::Window *pWindow)
static OUString getUILocale()
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
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)
static SfxViewFrame * Current()
std::unique_ptr< weld::Button > m_pNext
RET_OK
IMPL_LINK(TipOfTheDayDialog, OnLinkClick, weld::LinkButton &, rButton, bool)
Reference< XComponentContext > getProcessComponentContext()
OUString GetTooltipForCommand(const OUString &rsCommandName, const css::uno::Sequence< css::beans::PropertyValue > &rProperties, const Reference< frame::XFrame > &rxFrame)