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 <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>
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"))
41 m_pShowTip
->connect_toggled(LINK(this, TipOfTheDayDialog
, OnShowTipToggled
));
42 m_pNext
->connect_clicked(LINK(this, TipOfTheDayDialog
, OnNextClick
));
44 nNumberOfTips
= SAL_N_ELEMENTS(TIPOFTHEDAY_STRINGARRAY
);
46 nCurrentTip
= rand() % nNumberOfTips
;
50 TipOfTheDayDialog::~TipOfTheDayDialog()
52 std::shared_ptr
<comphelper::ConfigurationChanges
> xChanges(
53 comphelper::ConfigurationChanges::create());
54 const auto t0
= std::chrono::system_clock::now().time_since_epoch();
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
);
61 static bool file_exists(const OUString
& fileName
)
63 ::osl::File
aFile(fileName
);
64 return aFile
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
;
67 void TipOfTheDayDialog::UpdateTip()
70 OUString aText
= CuiResId(std::get
<0>(TIPOFTHEDAY_STRINGARRAY
[nCurrentTip
]));
71 m_pText
->set_label(aText
);
74 aLink
= std::get
<1>(TIPOFTHEDAY_STRINGARRAY
[nCurrentTip
]);
77 m_pLink
->set_visible(false);
79 else if (aLink
.startsWith("http"))
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>());
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
));
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";
104 if (GraphicFilter::LoadGraphic(aURL
+ aImage
, OUString(), aGraphic
) == ERRCODE_NONE
)
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();
115 IMPL_STATIC_LINK(TipOfTheDayDialog
, OnShowTipToggled
, weld::ToggleButton
&, rButton
, void)
117 std::shared_ptr
<comphelper::ConfigurationChanges
> xChanges(
118 comphelper::ConfigurationChanges::create());
119 officecfg::Office::Common::Misc::ShowTipOfTheDay::set(rButton
.get_active(), xChanges
);
123 IMPL_LINK_NOARG(TipOfTheDayDialog
, OnLinkClick
, weld::LinkButton
&, void)
125 Application::GetHelp()->Start(aLink
, static_cast<weld::Widget
*>(nullptr));
128 IMPL_LINK_NOARG(TipOfTheDayDialog
, OnNextClick
, weld::Button
&, void)
130 nCurrentTip
= (nCurrentTip
+ 1) % nNumberOfTips
;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */