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 <tipofthedaydlg.hxx>
21 
22 #include <dialmgr.hxx>
23 #include <officecfg/Office/Common.hxx>
24 #include <osl/file.hxx>
25 #include <rtl/bootstrap.hxx>
26 #include <tipoftheday.hrc>
27 #include <vcl/graphicfilter.hxx>
28 #include <vcl/help.hxx>
29 #include <vcl/virdev.hxx>
30 #include <vcl/svapp.hxx>
32 #include <unotools/resmgr.hxx>
33 #include <unotools/configmgr.hxx>
34 
36  : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
37  , m_pImage(m_xBuilder->weld_image("imImage"))
38  , m_pText(m_xBuilder->weld_label("lbText"))
39  , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
40  , m_pNext(m_xBuilder->weld_button("btnNext"))
41  , m_pLink(m_xBuilder->weld_link_button("btnLink"))
42 {
43  m_pShowTip->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
44  m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
45 
46  nNumberOfTips = SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY);
47  nCurrentTip = officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
48 
49  const auto t0 = std::chrono::system_clock::now().time_since_epoch();
50  nDay = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24; //days since 1970-01-01
51  if (nDay > officecfg::Office::Common::Misc::LastTipOfTheDayShown::get())
52  nCurrentTip++;
53 
54  UpdateTip();
55 }
56 
58 {
59  std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
61  officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(nDay, xChanges);
62  officecfg::Office::Common::Misc::LastTipOfTheDayID::set(nCurrentTip, xChanges);
63  officecfg::Office::Common::Misc::ShowTipOfTheDay::set(m_pShowTip->get_active(), xChanges);
64  xChanges->commit();
65 }
66 
67 static bool file_exists(const OUString& fileName)
68 {
69  ::osl::File aFile(fileName);
70  return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
71 }
72 
74 {
75  if ((nCurrentTip + 1 > nNumberOfTips) || (nCurrentTip < 0))
76  nCurrentTip = 0;
77  m_xDialog->set_title(CuiResId(STR_TITLE) + ": " + OUString::number(nCurrentTip + 1) + "/"
78  + OUString::number(nNumberOfTips));
79 
80  // text
81  OUString aText = CuiResId(std::get<0>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]));
82  m_pText->set_label(aText);
83 
84  // hyperlink
85  aLink = std::get<1>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
86  if (aLink.isEmpty())
87  {
88  m_pLink->set_visible(false);
89  }
90  else if (aLink.startsWith("http"))
91  {
92  // Links may have some %PRODUCTVERSION which need to be expanded
94  sal_Int32 aPos = aText.indexOf("%LANGUAGENAME");
95  if (aPos != -1)
96  {
98  if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
100  aText = aText.replaceAt(aPos, 13, aLang);
101  }
102  m_pLink->set_uri(aText);
103 
104  m_pLink->set_label(CuiResId(STR_MORE_LINK));
105  m_pLink->set_visible(true);
106  m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
107  }
108  else
109  {
110  m_pLink->set_uri("");
111  m_pLink->set_label(CuiResId(STR_HELP_LINK));
112  m_pLink->set_visible(true);
113  //converts aLink into the proper offline/online hyperlink
114  m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
115  }
116  // image
117  OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
118  rtl::Bootstrap::expandMacros(aURL);
119  OUString aImage = std::get<2>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
120  // use default image if none is available with the number
121  if (aImage.isEmpty() || !file_exists(aURL + aImage))
122  aImage = "tipoftheday.png";
123  // draw image
124  Graphic aGraphic;
125  if (GraphicFilter::LoadGraphic(aURL + aImage, OUString(), aGraphic) == ERRCODE_NONE)
126  {
127  ScopedVclPtr<VirtualDevice> m_pVirDev = m_pImage->create_virtual_device();
128  m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
129  m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
130  m_pImage->set_image(m_pVirDev.get());
131  m_pVirDev.disposeAndClear();
132  }
133 }
134 
136 {
137  Application::GetHelp()->Start(aLink, static_cast<weld::Widget*>(nullptr));
138  return true;
139 }
140 
142 {
143  nCurrentTip++; //zeroed at updatetip when out of range
144  UpdateTip();
145 }
146 
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
const size_t count(pCandidateA->getBorderLines().size())
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
OUString ExpandVariables(const OUString &rString)
static Help * GetHelp()
const OUString & getBcp47(bool bResolveSystem=true) const
std::unique_ptr< weld::Label > m_pText
std::unique_ptr< weld::Image > m_pImage
OUString getLanguage() const
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
std::unique_ptr< weld::LinkButton > m_pLink
#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
static bool file_exists(const OUString &fileName)
virtual bool Start(const OUString &rHelpId, const vcl::Window *pWindow)
TipOfTheDayDialog(weld::Window *pWindow)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
static OUString getUILocale()
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
virtual ~TipOfTheDayDialog() override
#define ERRCODE_NONE
std::unique_ptr< weld::Button > m_pNext
VirtualDevice * get() const
IMPL_LINK_NOARG(TipOfTheDayDialog, OnLinkClick, weld::LinkButton &, bool)