4 * @brief Implementation of manager_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include <utils/linked_list.h>
29 typedef struct private_manager_t private_manager_t
;
32 * private data of manager
34 struct private_manager_t
{
47 * user id, if we are logged in
58 * Implementation of manager_t.create_gateway_enumerator.
60 static enumerator_t
* create_gateway_enumerator(private_manager_t
*this)
62 return this->db
->create_gateway_enumerator(this->db
, this->user
);
66 * Implementation of manager_t.select_gateway.
68 static gateway_t
* select_gateway(private_manager_t
*this, int select_id
)
72 enumerator_t
*enumerator
;
77 if (this->gateway
) this->gateway
->destroy(this->gateway
);
80 enumerator
= this->db
->create_gateway_enumerator(this->db
, this->user
);
81 while (enumerator
->enumerate(enumerator
, &id
, &name
, &port
, &address
))
87 host
= host_create_from_string(address
, port
);
90 this->gateway
= gateway_create(name
, host
);
96 enumerator
->destroy(enumerator
);
102 * Implementation of manager_t.logged_in.
104 static bool logged_in(private_manager_t
*this)
106 return this->user
!= 0;
110 * Implementation of manager_t.login.
112 static bool login(private_manager_t
*this, char *username
, char *password
)
116 this->user
= this->db
->login(this->db
, username
, password
);
118 return this->user
!= 0;
122 * Implementation of manager_t.logout.
124 static void logout(private_manager_t
*this)
128 this->gateway
->destroy(this->gateway
);
129 this->gateway
= NULL
;
135 * Implementation of manager_t.destroy
137 static void destroy(private_manager_t
*this)
139 if (this->gateway
) this->gateway
->destroy(this->gateway
);
146 manager_t
*manager_create(database_t
*database
)
148 private_manager_t
*this = malloc_thing(private_manager_t
);
150 this->public.login
= (bool(*)(manager_t
*, char *username
, char *password
))login
;
151 this->public.logged_in
= (bool(*)(manager_t
*))logged_in
;
152 this->public.logout
= (void(*)(manager_t
*))logout
;
153 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(manager_t
*))create_gateway_enumerator
;
154 this->public.select_gateway
= (gateway_t
*(*)(manager_t
*, int id
))select_gateway
;
155 this->public.context
.destroy
= (void(*)(context_t
*))destroy
;
159 this->gateway
= NULL
;
161 return &this->public;