LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
actionresultreporter.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "actionresultreporter.h"
10 #include <QApplication>
11 #include <QMessageBox>
13 #include <util/sll/visitor.h>
14 #include <util/xpc/util.h>
15 
16 namespace LC::Util
17 {
18  using namespace std::chrono_literals;
19 
20  namespace
21  {
22  QDeadlineTimer GetDeadlineTimer (ActionResultReporter::BackgroundPolicy policy)
23  {
24  return Visit (policy,
25  [] (const ActionResultReporter::TimeoutBackgroundPolicy& policy) { return QDeadlineTimer { policy.Timeout_ }; },
26  [] (const auto&) { return QDeadlineTimer { QDeadlineTimer::Forever }; });
27  }
28  }
29 
31  : IEM_ { iem }
32  , Context_ { config.Context_ }
33  , Priority_ { config.Priority_ }
34  , Parent_ { parent }
35  , TimerBackground_ { GetDeadlineTimer (config.BackgroundPolicy_) }
36  {
37  Visit (config.BackgroundPolicy_,
38  [] (const TimeoutBackgroundPolicy&) {},
39  [this] (const auto&)
40  {
41  InitialFocus_ = qApp->focusWidget ();
42  });
43  }
44 
45  void ActionResultReporter::operator() (const QString& error)
46  {
47  const auto isBackground = FocusChanged () || TimerBackground_.hasExpired () || (!Parent_ && HadParent_);
48 
49  if (isBackground)
50  IEM_.HandleEntity (MakeNotification (Context_, error, Priority_));
51  else
52  switch (Priority_)
53  {
54  case Priority::Info:
55  QMessageBox::information (Parent_, Context_, error);
56  break;
57  case Priority::Warning:
58  QMessageBox::warning (Parent_, Context_, error);
59  break;
60  case Priority::Critical:
61  QMessageBox::critical (Parent_, Context_, error);
62  break;
63  }
64  }
65 
66  bool ActionResultReporter::FocusChanged () const
67  {
68  if (!InitialFocus_ || !*InitialFocus_)
69  return false;
70 
71  const auto curFocus = qApp->focusWidget ();
72  return InitialFocus_ != curFocus;
73  }
74 }
Proxy to core entity manager.
virtual bool HandleEntity(LC::Entity entity, QObject *desired=nullptr)=0
Handles the given entity.
std::variant< DefaultBackgroundPolicy, FocusBackgroundPolicy, std::chrono::milliseconds > BackgroundPolicy
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:180
ActionResultReporter(IEntityManager &iem, Config config, QWidget *parent=nullptr)
Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:95