xref: /core/cui/source/dialogs/tipofthedaydlg.cxx (revision d1d44e1d)
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 <config_folders.h>
23 #include <dialmgr.hxx>
24 #include <officecfg/Office/Common.hxx>
25 #include <osl/file.hxx>
26 #include <rtl/bootstrap.hxx>
27 #include <tipoftheday.hrc>
28 #include <vcl/graphicfilter.hxx>
29 #include <vcl/virdev.hxx>
30 #include <sfx2/sfxhelp.hxx>
31 #include <vcl/svapp.hxx>
32 
33 TipOfTheDayDialog::TipOfTheDayDialog(weld::Window* pParent)
34     : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
35     , m_pImage(m_xBuilder->weld_image("imImage"))
36     , m_pText(m_xBuilder->weld_label("lbText"))
37     , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
38     , m_pNext(m_xBuilder->weld_button("btnNext"))
39     , m_pLink(m_xBuilder->weld_link_button("btnLink"))
40 {
41     m_pShowTip->connect_toggled(LINK(this, TipOfTheDayDialog, OnShowTipToggled));
42     m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
43 
44     nNumberOfTips = SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY);
45     srand(time(nullptr));
46     nCurrentTip = rand() % nNumberOfTips;
47     UpdateTip();
48 }
49 
50 TipOfTheDayDialog::~TipOfTheDayDialog()
51 {
52     std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
53         comphelper::ConfigurationChanges::create());
54     const auto t0 = std::chrono::system_clock::now().time_since_epoch();
55     const sal_Int32 nDay
56         = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24; // days since 1970-01-01
57     officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(nDay, xChanges);
58     xChanges->commit();
59 }
60 
61 static bool file_exists(const OUString& fileName)
62 {
63     ::osl::File aFile(fileName);
64     return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
65 }
66 
67 void TipOfTheDayDialog::UpdateTip()
68 {
69     // text
70     OUString aText = CuiResId(std::get<0>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]));
71     m_pText->set_label(aText);
72 
73     // hyperlink
74     aLink = std::get<1>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
75     if (aLink.isEmpty())
76     {
77         m_pLink->set_visible(false);
78     }
79     else if (aLink.startsWith("http"))
80     {
81         m_pLink->set_uri(aLink);
82         m_pLink->set_label(CuiResId(STR_MORE_LINK));
83         m_pLink->set_visible(true);
84         m_pLink->connect_clicked(Link<weld::LinkButton&, void>());
85     }
86     else
87     {
88         m_pLink->set_uri("");
89         m_pLink->set_label(CuiResId(STR_HELP_LINK));
90         m_pLink->set_visible(true);
91         //converts aLink into the proper offline/online hyperlink
92         m_pLink->connect_clicked(LINK(this, TipOfTheDayDialog, OnLinkClick));
93     }
94 
95     // image
96     OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
97     rtl::Bootstrap::expandMacros(aURL);
98     OUString aImage = std::get<2>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
99     // use default image if none is available with the number
100     if (aImage.isEmpty() || !file_exists(aURL + aImage))
101         aImage = "tipoftheday.png";
102     // draw image
103     Graphic aGraphic;
104     if (GraphicFilter::LoadGraphic(aURL + aImage, OUString(), aGraphic) == ERRCODE_NONE)
105     {
106         ScopedVclPtr<VirtualDevice> m_pVirDev;
107         m_pVirDev = m_pImage->create_virtual_device();
108         m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
109         m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
110         m_pImage->set_image(m_pVirDev.get());
111         m_pVirDev.disposeAndClear();
112     }
113 }
114 
115 IMPL_STATIC_LINK(TipOfTheDayDialog, OnShowTipToggled, weld::ToggleButton&, rButton, void)
116 {
117     std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
118         comphelper::ConfigurationChanges::create());
119     officecfg::Office::Common::Misc::ShowTipOfTheDay::set(rButton.get_active(), xChanges);
120     xChanges->commit();
121 }
122 
123 IMPL_LINK_NOARG(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, void)
124 {
125     Application::GetHelp()->Start(aLink, static_cast<weld::Widget*>(nullptr));
126 }
127 
128 IMPL_LINK_NOARG(TipOfTheDayDialog, OnNextClick, weld::Button&, void)
129 {
130     nCurrentTip = (nCurrentTip + 1) % nNumberOfTips;
131     UpdateTip();
132 }
133 
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
135