blob: 6523955ececd5558986573ac71fc984f7efe259f [file] [log] [blame]
Paul Stewart0756db92012-01-27 08:34:47 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/profile_dbus_property_exporter.h"
6
7#include <string>
8
9#include <base/basictypes.h>
10#include <chromeos/dbus/service_constants.h>
11#include <dbus-c++/dbus.h>
12
13#include "shill/dbus_adaptor.h"
14#include "shill/error.h"
15#include "shill/service.h"
16#include "shill/store_interface.h"
17#include "shill/technology.h"
18#include "shill/wifi_service.h"
19
20using std::string;
21
22namespace shill {
23
24ProfileDBusPropertyExporter::ProfileDBusPropertyExporter(
25 const StoreInterface *storage, const string &entry_name)
26 : storage_(storage), entry_name_(entry_name) {}
27
28ProfileDBusPropertyExporter::~ProfileDBusPropertyExporter() {}
29
30bool ProfileDBusPropertyExporter::LoadServiceProperties(
31 PropertyList *properties, Error *error) {
32 if (!storage_->ContainsGroup(entry_name_)) {
33 Error::PopulateAndLog(
34 error, Error::kNotFound,
35 "Could not find profile entry: " + entry_name_);
36 return false;
37 }
38
39 Technology::Identifier technology =
40 Technology::IdentifierFromStorageGroup(entry_name_);
41
42 if (technology == Technology::kUnknown) {
43 Error::PopulateAndLog(
44 error, Error::kInternalError,
45 "Could not determine technology for entry: " + entry_name_);
46 return false;
47 }
48
49 if (technology == Technology::kWifi) {
50 LoadWiFiServiceProperties(properties, error);
51 }
52
53 LoadBool(properties, Service::kStorageAutoConnect,
54 flimflam::kAutoConnectProperty);
55 LoadString(properties, Service::kStorageError, flimflam::kErrorProperty);
56 LoadString(properties, Service::kStorageGUID, flimflam::kGuidProperty);
57 LoadString(properties, Service::kStorageName, flimflam::kNameProperty);
58 if (!LoadString(properties, Service::kStorageType, flimflam::kTypeProperty)) {
59 SetString(properties, flimflam::kTypeProperty,
60 Technology::NameFromIdentifier(technology));
61 }
62 LoadString(properties, Service::kStorageUIData, flimflam::kUIDataProperty);
63 return true;
64}
65
66bool ProfileDBusPropertyExporter::LoadWiFiServiceProperties(
67 PropertyList *properties,
68 Error */*error*/) {
69 LoadBool(properties, WiFiService::kStorageHiddenSSID,
70 flimflam::kWifiHiddenSsid);
71
72 // Support the old and busted technique for storing "Mode" and "Security"
73 // within the entry name.
74 string address;
75 string mode;
76 string security;
77 bool parsed_identifier = WiFiService::ParseStorageIdentifier(
78 entry_name_, &address, &mode, &security);
79
80 if (!LoadString(properties, WiFiService::kStorageMode,
81 flimflam::kModeProperty) && parsed_identifier) {
82 SetString(properties, flimflam::kModeProperty, mode);
83 }
84
85 if (!LoadString(properties, WiFiService::kStorageSecurity,
86 flimflam::kSecurityProperty) && parsed_identifier) {
87 SetString(properties, flimflam::kSecurityProperty, security);
88 }
89 return true;
90}
91
92bool ProfileDBusPropertyExporter::LoadBool(PropertyList *properties,
93 const string &storage_name,
94 const string &dbus_name) {
95 bool value;
96 if (!storage_->GetBool(entry_name_, storage_name, &value)) {
97 return false;
98 }
99
100 SetBool(properties, dbus_name, value);
101 return true;
102}
103
104bool ProfileDBusPropertyExporter::LoadString(PropertyList *properties,
105 const string &storage_name,
106 const string &dbus_name) {
107 string value;
108 if (!storage_->GetString(entry_name_, storage_name, &value)) {
109 return false;
110 }
111
112 SetString(properties, dbus_name, value);
113 return true;
114}
115
116void ProfileDBusPropertyExporter::SetBool(PropertyList *properties,
117 const string &dbus_name,
118 bool value) {
119 (*properties)[dbus_name] = DBusAdaptor::BoolToVariant(value);
120}
121
122void ProfileDBusPropertyExporter::SetString(PropertyList *properties,
123 const string &dbus_name,
124 const string &value) {
125 (*properties)[dbus_name] = DBusAdaptor::StringToVariant(value);
126}
127
128} // namespace shill