1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <tipofthedaydlg.hxx>
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>
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"))
43 m_pShowTip
->set_active(officecfg::Office::Common::Misc::ShowTipOfTheDay::get());
44 m_pNext
->connect_clicked(LINK(this, TipOfTheDayDialog
, OnNextClick
));
46 nNumberOfTips
= SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY
);
47 nCurrentTip
= officecfg::Office::Common::Misc::LastTipOfTheDayID::get();
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())
57 TipOfTheDayDialog::~TipOfTheDayDialog()
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
);
67 static bool file_exists(const OUString
& fileName
)
69 ::osl::File
aFile(fileName
);
70 return aFile
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
;
73 void TipOfTheDayDialog::UpdateTip()
75 if ((nCurrentTip
+ 1 > nNumberOfTips
) || (nCurrentTip
< 0))
77 m_xDialog
->set_title(CuiResId(STR_TITLE
) + ": " + OUString::number(nCurrentTip
+ 1) + "/"
78 + OUString::number(nNumberOfTips
));
81 OUString aText
= CuiResId(std::get
<0>(TIPOFTHEDAY_STRINGARRAY
[nCurrentTip
]));
82 m_pText
->set_label(aText
);
85 aLink
= std::get
<1>(TIPOFTHEDAY_STRINGARRAY
[nCurrentTip
]);
88 m_pLink
->set_visible(false);
90 else if (aLink
.startsWith("http"))
92 // Links may have some %PRODUCTVERSION which need to be expanded
93 aText
= Translate::ExpandVariables(aLink
);
94 sal_Int32 aPos
= aText
.indexOf("%LANGUAGENAME");
97 OUString aLang
= LanguageTag(utl::ConfigManager::getUILocale()).getLanguage();
98 if (aLang
== "en" || aLang
== "pt" || aLang
== "zh") //en-US/GB, pt-BR, zh-CH/TW
99 aLang
= LanguageTag(utl::ConfigManager::getUILocale()).getBcp47();
100 aText
= aText
.replaceAt(aPos
, 13, aLang
);
102 m_pLink
->set_uri(aText
);
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>());
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
));
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";
125 if (GraphicFilter::LoadGraphic(aURL
+ aImage
, OUString(), aGraphic
) == ERRCODE_NONE
)
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();
135 IMPL_LINK_NOARG(TipOfTheDayDialog
, OnLinkClick
, weld::LinkButton
&, bool)
137 Application::GetHelp()->Start(aLink
, static_cast<weld::Widget
*>(nullptr));
141 IMPL_LINK_NOARG(TipOfTheDayDialog
, OnNextClick
, weld::Button
&, void)
143 nCurrentTip
++; //zeroed at updatetip when out of range
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */