libyui  3.3.1
YExternalWidgets.cc
1 /*
2  Copyright (C) 2013 Angelo Naselli <anaselli at linux dot it>
3 
4  This file is part of libyui project
5 
6  This library is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as
8  published by the Free Software Foundation; either version 2.1 of the
9  License, or (at your option) version 3.0 of the License. This library
10  is distributed in the hope that it will be useful, but WITHOUT ANY
11  WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  License for more details. You should have received a copy of the GNU
14  Lesser General Public License along with this library; if not, write
15  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
16  Floor, Boston, MA 02110-1301 USA
17 */
18 #define YUILogComponent "ew"
19 #include "YUILog.h"
20 
21 #include "YUI.h"
22 #include "YUILoader.h"
23 #include "YUIException.h"
24 #include "YExternalWidgets.h"
25 #include "YExternalWidgetFactory.h"
26 
27 #include <map>
28 #include <string>
29 
30 std::map<std::string, YExternalWidgets *> YExternalWidgets::_externalWidgets;
31 
32 YExternalWidgets::YExternalWidgets(const std::string& name) : _name(name), _factory(0)
33 {
34  if (!YUI::ui())
35  YUI_THROW( YUIException( "UI must be initialized first" ) );
36 
37  yuiMilestone() << "Creating Libyui External Widgets object" << std::endl;
38 
39  std::pair<std::map<std::string, YExternalWidgets *>::iterator, bool> ret;
40  ret = _externalWidgets.insert ( std::pair<std::string, YExternalWidgets *>(_name, this));
41  if (ret.second==false) {
42  std::string errorString = _name;
43  errorString.append(" already created");
44  YUI_THROW( YUIException( errorString ) );
45  }
46 }
47 
49 {
50  delete _factory;
51 
52  _externalWidgets.erase(_name);
53 }
54 
56 {
57  std::map<std::string, YExternalWidgets *>::iterator it;
58 
59  if (!YUI::ui())
60  YUI_THROW( YUIException( "UI must be initialized first" ) );
61 
62  it = _externalWidgets.find(name);
63  if (it == _externalWidgets.end())
64  {
66  }
67 
68  return _externalWidgets[name];
69 }
70 
72 {
74 }
75 
77 {
78  if (!YUI::ui())
79  YUI_THROW( YUIException( "UI must be initialized first" ) );
80 
81  if ( !_factory )
82  _factory = this->createExternalWidgetFactory();
83 
84  YUI_CHECK_PTR( _factory );
85 
86  return _factory;
87 }
88 
89 
90 /**
91  * Helper class to make sure the EW is properly shut down.
92  **/
94 {
95 public:
97 
98  /**
99  * Destructor.
100  *
101  * If there still is a EW, it will be deleted.
102  * If there is none, this will do nothing.
103  **/
105 };
106 
107 
109 {
110  // Let's copy map to avoid content deletion when removing ExternalWidgets objects
111  std::map <std::string, YExternalWidgets* > ew = YExternalWidgets::_externalWidgets;
112  std::map<std::string, YExternalWidgets *>::iterator it;
113 
114  for (it= ew.begin(); it != ew.end(); it++)
115  {
116  yuiMilestone() << "Shutting down " << it->first << " External Widgets" << std::endl;
117  delete it->second;
118  }
119 }
120 
121 
122 /**
123  * Static YExternalWidgetsTerminator instance: It will make sure the EW is deleted in its
124  * global destructor. If the EW is already destroyed, it will do nothing. If
125  * there still is a EW object, it will be deleted.
126  *
127  * This is particularly important for the NCurses EW so the terminal settings
128  * are properly restored.
129  **/
130 static YExternalWidgetsTerminator weTerminator;
131 
~YExternalWidgetsTerminator()
Destructor.
virtual YExternalWidgetFactory * createExternalWidgetFactory()=0
Create the external widgets factory that provides all the createXY() methods for. ...
YExternalWidgets(const std::string &name)
Constructor.
Helper class to make sure the EW is properly shut down.
static YExternalWidgets * externalWidgets(const std::string &name)
Access the global YUI external widgets.
static void loadExternalWidgets(const std::string &name, const std::string &symbol="_Z21createExternalWidgetsPKc")
Load the given External Widgets plugin followed by its graphical extension implementation in the foll...
Definition: YUILoader.cc:226
Abstract base class of a libYUI Widget Extension interface.
virtual ~YExternalWidgets()
Destructor.
YExternalWidgetFactory * externalWidgetFactory()
Return the external widget factory that provides all the createXY() methods for user defined widgets...
static YUI * ui()
Access the global UI.
Definition: YUI.cc:118
Base class for UI Exceptions.
Definition: YUIException.h:297
Abstract widget factory for mandatory widgets.