xref: /core/cui/source/dialogs/tipofthedaydlg.cxx (revision 1bc50aca)
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>
31 #include <i18nlangtag/languagetag.hxx>
32 #include <unotools/resmgr.hxx>
33 #include <unotools/configmgr.hxx>
34 
35 TipOfTheDayDialog::TipOfTheDayDialog(weld::Window* pParent)
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 
57 TipOfTheDayDialog::~TipOfTheDayDialog()
58 {
59     std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
60         comphelper::ConfigurationChanges::create());
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 
73 void TipOfTheDayDialog::UpdateTip()
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 //replace MOD1 & MOD2 shortcuts depending on platform
83 #ifdef MACOSX
84     const OUString aMOD1 = CuiResId(STR_CMD);
85     const OUString aMOD2 = CuiResId(STR_Option);
86 #else
87     const OUString aMOD1 = CuiResId(STR_CTRL);
88     const OUString aMOD2 = CuiResId(STR_Alt);
89 #endif
90     sal_Int32 aPos;
91     aPos = aText.indexOf("%MOD1");
92     if (aPos != -1)
93         aText = aText.replaceAt(aPos, 5, aMOD1);
94     aPos = aText.indexOf("%MOD2");
95     if (aPos != -1)
96         aText = aText.replaceAt(aPos, 5, aMOD2);
97     m_pText->set_label(aText);
98 
99     // hyperlink
100     aLink = std::get<1>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
101     if (aLink.isEmpty())
102     {
103         m_pLink->set_visible(false);
104     }
105     else if (aLink.startsWith("http"))
106     {
107         // Links may have some %PRODUCTVERSION which need to be expanded
108         aText = Translate::ExpandVariables(aLink);
109         aPos = aText.indexOf("%LANGUAGENAME");
110         if (aPos != -1)
111         {
112             OUString aLang = LanguageTag(utl::ConfigManager::getUILocale()).getLanguage();
113             if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
114                 aLang = LanguageTag(utl::ConfigManager::getUILocale()).getBcp47();
115             aText = aText.replaceAt(aPos, 13, aLang);
116         }
117         m_pLink->set_uri(aText);
118 
119         m_pLink->set_label(CuiResId(STR_MORE_LINK));
120         m_pLink->set_visible(true);
121         m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
122     }
123     else
124     {
125         m_pLink->set_uri("");
126         m_pLink->set_label(CuiResId(STR_HELP_LINK));
127         m_pLink->set_visible(true);
128         //converts aLink into the proper offline/online hyperlink
129         m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
130     }
131     // image
132     OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
133     rtl::Bootstrap::expandMacros(aURL);
134     OUString aImage = std::get<2>(TIPOFTHEDAY_STRINGARRAY[nCurrentTip]);
135     // use default image if none is available with the number
136     if (aImage.isEmpty() || !file_exists(aURL + aImage))
137         aImage = "tipoftheday.png";
138     // draw image
139     Graphic aGraphic;
140     if (GraphicFilter::LoadGraphic(aURL + aImage, OUString(), aGraphic) == ERRCODE_NONE)
141     {
142         ScopedVclPtr<VirtualDevice> m_pVirDev = m_pImage->create_virtual_device();
143         m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
144         m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
145         m_pImage->set_image(m_pVirDev.get());
146         m_pVirDev.disposeAndClear();
147     }
148 }
149 
150 IMPL_LINK_NOARG(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, bool)
151 {
152     Application::GetHelp()->Start(aLink, static_cast<weld::Widget*>(nullptr));
153     return true;
154 }
155 
156 IMPL_LINK_NOARG(TipOfTheDayDialog, OnNextClick, weld::Button&, void)
157 {
158     nCurrentTip++; //zeroed at updatetip when out of range
159     UpdateTip();
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
163