Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cui / source / dialogs / tipofthedaydlg.cxx
blobf1cb7afc2c1ff4742b5245de4622dec9497e5e8c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <tipofthedaydlg.hxx>
22 #include <tipoftheday.hrc>
24 #include <sfx2/viewfrm.hxx>
25 #include <vcl/commandinfoprovider.hxx>
26 #include <vcl/graphicfilter.hxx>
27 #include <vcl/help.hxx>
28 #include <vcl/window.hxx>
29 #include <vcl/ImageTree.hxx>
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>
36 #include <comphelper/dispatchcommand.hxx>
37 #include <dialmgr.hxx>
38 #include <i18nlangtag/languagetag.hxx>
39 #include <officecfg/Office/Common.hxx>
40 #include <osl/file.hxx>
41 #include <rtl/bootstrap.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
43 #include <unotools/resmgr.hxx>
44 #include <unotools/configmgr.hxx>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <bitmaps.hlst>
48 //size of preview
49 const Size ThumbSize(150, 150);
51 TipOfTheDayDialog::TipOfTheDayDialog(weld::Window* pParent)
52 : GenericDialogController(pParent, "cui/ui/tipofthedaydialog.ui", "TipOfTheDayDialog")
53 , m_pParent(pParent)
54 , m_pText(m_xBuilder->weld_label("lbText"))
55 , m_pShowTip(m_xBuilder->weld_check_button("cbShowTip"))
56 , m_pNext(m_xBuilder->weld_button("btnNext"))
57 , m_pLink(m_xBuilder->weld_link_button("btnLink"))
58 , m_pPreview(new weld::CustomWeld(*m_xBuilder, "imPreview", m_aPreview))
60 m_pShowTip->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
61 m_pNext->connect_clicked(LINK(this, TipOfTheDayDialog, OnNextClick));
62 m_nCurrentTip = officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
63 m_pPreview->set_size_request(ThumbSize.Width(), ThumbSize.Height());
65 if (pParent != nullptr)
67 css::uno::Reference<css::awt::XWindow> xWindow = pParent->GetXWindow();
68 if (xWindow.is())
70 VclPtr<vcl::Window> xVclWin(VCLUnoHelper::GetWindow(xWindow));
71 if (xVclWin != nullptr)
72 xVclWin->AddEventListener(LINK(this, TipOfTheDayDialog, Terminated));
76 const auto t0 = std::chrono::system_clock::now().time_since_epoch();
77 sal_Int32 nDay = std::chrono::duration_cast<std::chrono::hours>(t0).count() / 24;
78 //show next tip after one day
79 if (nDay > officecfg::Office::Common::Misc::LastTipOfTheDayShown::get())
80 m_nCurrentTip++;
82 // save this time to the config now instead of in the dtor otherwise we
83 // end up with multiple copies of this dialog every time we open a new
84 // document if the first one isn't closed
85 std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
86 comphelper::ConfigurationChanges::create());
87 officecfg::Office::Common::Misc::LastTipOfTheDayShown::set(nDay, xChanges);
88 xChanges->commit();
90 UpdateTip();
93 IMPL_LINK(TipOfTheDayDialog, Terminated, VclWindowEvent&, rEvent, void)
95 if (rEvent.GetId() == VclEventId::ObjectDying)
97 m_pParent = nullptr;
98 TipOfTheDayDialog::response(RET_OK);
102 TipOfTheDayDialog::~TipOfTheDayDialog()
104 std::shared_ptr<comphelper::ConfigurationChanges> xChanges(
105 comphelper::ConfigurationChanges::create());
106 officecfg::Office::Common::Misc::LastTipOfTheDayID::set(m_nCurrentTip, xChanges);
107 officecfg::Office::Common::Misc::ShowTipOfTheDay::set(m_pShowTip->get_active(), xChanges);
108 xChanges->commit();
110 if (m_pParent != nullptr)
112 css::uno::Reference<css::awt::XWindow> xWindow = m_pParent->GetXWindow();
113 if (xWindow.is())
115 VclPtr<vcl::Window> xVclWin(VCLUnoHelper::GetWindow(xWindow));
116 if (xVclWin != nullptr)
117 xVclWin->RemoveEventListener(LINK(this, TipOfTheDayDialog, Terminated));
122 static bool file_exists(const OUString& fileName)
124 ::osl::File aFile(fileName);
125 return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
128 void TipOfTheDayDialog::UpdateTip()
130 constexpr sal_Int32 nNumberOfTips = std::size(TIPOFTHEDAY_STRINGARRAY);
132 if ((m_nCurrentTip >= nNumberOfTips) || (m_nCurrentTip < 0))
133 m_nCurrentTip = 0;
135 //title
136 m_xDialog->set_title(CuiResId(STR_TITLE)
137 .replaceFirst("%CURRENT", OUString::number(m_nCurrentTip + 1))
138 .replaceFirst("%TOTAL", OUString::number(nNumberOfTips)));
140 auto[sTip, sLink, sImage, nType] = TIPOFTHEDAY_STRINGARRAY[m_nCurrentTip];
142 // text
143 //replace MOD1 & MOD2 shortcuts depending on platform
144 #ifdef MACOSX
145 const OUString aMOD1 = CuiResId(STR_CMD);
146 const OUString aMOD2 = CuiResId(STR_Option);
147 #else
148 const OUString aMOD1 = CuiResId(STR_CTRL);
149 const OUString aMOD2 = CuiResId(STR_Alt);
150 #endif
151 m_pText->set_label(CuiResId(sTip).replaceAll("%MOD1", aMOD1).replaceAll("%MOD2", aMOD2));
153 // hyperlink
154 if (sLink.isEmpty())
156 m_pLink->set_visible(false);
158 else if (sLink.startsWith(".uno:"))
160 m_pLink->set_visible(false);
161 //show the link only if the UNO command is available in the current module
162 if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
164 const auto xFrame = pViewFrame->GetFrame().GetFrameInterface();
165 const css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(
166 xFrame, css::uno::UNO_QUERY);
167 if (xDispatchProvider.is())
169 css::util::URL aCommandURL;
170 aCommandURL.Complete = sLink;
171 const css::uno::Reference<css::uno::XComponentContext> xContext
172 = comphelper::getProcessComponentContext();
173 const css::uno::Reference<css::util::XURLTransformer> xParser
174 = css::util::URLTransformer::create(xContext);
175 xParser->parseStrict(aCommandURL);
176 const css::uno::Reference<css::frame::XDispatch> xDisp
177 = xDispatchProvider->queryDispatch(aCommandURL, OUString(), 0);
178 if (xDisp.is())
180 m_pLink->set_label(CuiResId(STR_UNO_LINK));
181 m_pLink->set_uri(sLink);
183 const OUString aModuleName(
184 vcl::CommandInfoProvider::GetModuleIdentifier(xFrame));
185 const auto aProperties
186 = vcl::CommandInfoProvider::GetCommandProperties(sLink, aModuleName);
187 m_pLink->set_tooltip_text(
188 vcl::CommandInfoProvider::GetTooltipForCommand(sLink, aProperties, xFrame));
190 m_pLink->set_visible(true);
191 m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
196 else if (sLink.startsWith("http"))
198 // Links may have some %PRODUCTVERSION which need to be expanded
199 OUString aText = Translate::ExpandVariables(sLink);
200 OUString aLang = LanguageTag(utl::ConfigManager::getUILocale()).getLanguage();
201 if (aLang == "en" || aLang == "pt" || aLang == "zh") //en-US/GB, pt-BR, zh-CH/TW
202 aLang = LanguageTag(utl::ConfigManager::getUILocale()).getBcp47();
203 m_pLink->set_uri(aText.replaceFirst("%LANGUAGENAME", aLang));
204 m_pLink->set_label(CuiResId(STR_MORE_LINK));
205 m_pLink->set_visible(true);
206 m_pLink->connect_activate_link(Link<weld::LinkButton&, bool>());
208 else
210 m_pLink->set_uri(sLink);
211 m_pLink->set_label(CuiResId(STR_HELP_LINK));
212 m_pLink->set_visible(true);
213 m_pLink->connect_activate_link(LINK(this, TipOfTheDayDialog, OnLinkClick));
215 // image
216 OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/tipoftheday/");
217 rtl::Bootstrap::expandMacros(aURL);
218 OUString aImageName = sImage;
219 Graphic aGraphic;
221 if (!aImageName.isEmpty() && file_exists(aURL + aImageName))
222 GraphicFilter::LoadGraphic(aURL + aImageName, OUString(), aGraphic);
223 else
225 const OUString sModuleImage[5]
226 = { RID_SVXBMP_TOTD_WRITER, RID_SVXBMP_TOTD_CALC, RID_SVXBMP_TOTD_DRAW,
227 RID_SVXBMP_TOTD_IMPRESS, RID_SVXBMP_TOTD_SOFFICE };
228 const OUString aIconTheme
229 = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
230 BitmapEx aBmpEx;
231 ImageTree::get().loadImage(sModuleImage[nType], aIconTheme, aBmpEx, true,
232 ImageLoadFlags::IgnoreDarkTheme);
233 aGraphic = aBmpEx;
236 if (!aGraphic.IsAnimated())
238 BitmapEx aBmpEx(aGraphic.GetBitmapEx());
239 if (aBmpEx.Scale(ThumbSize))
240 aGraphic = aBmpEx;
242 m_aPreview.SetPreview(aGraphic);
245 IMPL_LINK(TipOfTheDayDialog, OnLinkClick, weld::LinkButton&, rButton, bool)
247 const OUString sLink = rButton.get_uri();
248 if (sLink.startsWith(".uno:"))
250 comphelper::dispatchCommand(sLink, {});
251 TipOfTheDayDialog::response(RET_OK);
253 else
255 Application::GetHelp()->Start(sLink, static_cast<weld::Widget*>(nullptr));
257 return true;
260 IMPL_LINK_NOARG(TipOfTheDayDialog, OnNextClick, weld::Button&, void)
262 m_nCurrentTip++; //zeroed at updatetip when out of range
263 UpdateTip();
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */