libyui  3.10.0
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 
29 using std::string;
30 using std::map;
31 
32 map<string, YExternalWidgets *> YExternalWidgets::_externalWidgets;
33 
34 
35 YExternalWidgets::YExternalWidgets(const string& name) : _name(name), _factory(0)
36 {
37  if (!YUI::ui())
38  YUI_THROW( YUIException( "UI must be initialized first" ) );
39 
40  yuiMilestone() << "Creating Libyui External Widgets object" << endl;
41 
42  std::pair<map<string, YExternalWidgets *>::iterator, bool> ret;
43  ret = _externalWidgets.insert ( std::pair<string, YExternalWidgets *>(_name, this));
44  if (ret.second==false) {
45  string errorString = _name;
46  errorString.append(" already created");
47  YUI_THROW( YUIException( errorString ) );
48  }
49 }
50 
51 
53 {
54  delete _factory;
55 
56  _externalWidgets.erase(_name);
57 }
58 
59 
61 {
62  map<string, YExternalWidgets *>::iterator it;
63 
64  if (!YUI::ui())
65  YUI_THROW( YUIException( "UI must be initialized first" ) );
66 
67  it = _externalWidgets.find(name);
68  if (it == _externalWidgets.end())
69  {
71  }
72 
73  return _externalWidgets[name];
74 }
75 
76 
78 {
80 }
81 
82 
84 {
85  if (!YUI::ui())
86  YUI_THROW( YUIException( "UI must be initialized first" ) );
87 
88  if ( !_factory )
89  _factory = this->createExternalWidgetFactory();
90 
91  YUI_CHECK_PTR( _factory );
92 
93  return _factory;
94 }
95 
96 
97 /**
98  * Helper class to make sure the EW is properly shut down.
99  **/
101 {
102 public:
104 
105  /**
106  * Destructor.
107  *
108  * If there still is a EW, it will be deleted.
109  * If there is none, this will do nothing.
110  **/
112 };
113 
114 
116 {
117  // Let's copy map to avoid content deletion when removing ExternalWidgets objects
118  map <string, YExternalWidgets* > ew = YExternalWidgets::_externalWidgets;
119  map<string, YExternalWidgets *>::iterator it;
120 
121  for ( it= ew.begin(); it != ew.end(); it++)
122  {
123  yuiMilestone() << "Shutting down " << it->first << " External Widgets" << endl;
124  delete it->second;
125  }
126 }
127 
128 
129 /**
130  * Static YExternalWidgetsTerminator instance: It will make sure the EW is deleted in its
131  * global destructor. If the EW is already destroyed, it will do nothing. If
132  * there still is a EW object, it will be deleted.
133  *
134  * This is particularly important for the NCurses EW so the terminal settings
135  * are properly restored.
136  **/
137 static YExternalWidgetsTerminator weTerminator;
138 
Abstract widget factory for mandatory widgets.
Helper class to make sure the EW is properly shut down.
~YExternalWidgetsTerminator()
Destructor.
Abstract base class of a libYUI Widget Extension interface.
YExternalWidgetFactory * externalWidgetFactory()
Return the external widget factory that provides all the createXY() methods for user defined widgets.
YExternalWidgets(const std::string &name)
Constructor.
virtual YExternalWidgetFactory * createExternalWidgetFactory()=0
Create the external widgets factory that provides all the createXY() methods for.
virtual ~YExternalWidgets()
Destructor.
static YExternalWidgets * externalWidgets(const std::string &name)
Access the global YUI external widgets.
Base class for UI Exceptions.
Definition: YUIException.h:298
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:295
static YUI * ui()
Access the global UI.
Definition: YUI.cc:124