1 // ExtendedServerInfo.cpp 2 3 #include "ExtendedServerInfo.h" 4 #include "ServerInfo.h" 5 6 // constructor ExtendedShareInfo()7ExtendedShareInfo::ExtendedShareInfo() 8 : 9 BReferenceable(), 10 fShareName() 11 { 12 } 13 14 // SetTo 15 status_t SetTo(const ShareInfo * shareInfo)16ExtendedShareInfo::SetTo(const ShareInfo* shareInfo) 17 { 18 if (!shareInfo) 19 return B_BAD_VALUE; 20 if (!fShareName.SetTo(shareInfo->GetShareName())) 21 return B_NO_MEMORY; 22 return B_OK; 23 } 24 25 // GetShareName 26 const char* GetShareName() const27ExtendedShareInfo::GetShareName() const 28 { 29 return fShareName.GetString(); 30 } 31 32 33 // #pragma mark - 34 35 // constructor ExtendedServerInfo(const NetAddress & address)36ExtendedServerInfo::ExtendedServerInfo(const NetAddress& address) 37 : 38 BReferenceable(), 39 fAddress(address), 40 fState(0) 41 { 42 } 43 44 // destructor ~ExtendedServerInfo()45ExtendedServerInfo::~ExtendedServerInfo() 46 { 47 int32 count = CountShares(); 48 for (int32 i = 0; i < count; i++) 49 ShareInfoAt(i)->ReleaseReference(); 50 } 51 52 // GetAddress 53 const NetAddress& GetAddress() const54ExtendedServerInfo::GetAddress() const 55 { 56 return fAddress; 57 } 58 59 // GetServerName 60 const char* GetServerName() const61ExtendedServerInfo::GetServerName() const 62 { 63 return fServerName.GetString(); 64 } 65 66 // GetConnectionMethod 67 const char* GetConnectionMethod() const68ExtendedServerInfo::GetConnectionMethod() const 69 { 70 return fConnectionMethod.GetString(); 71 } 72 73 // CountShares 74 int32 CountShares() const75ExtendedServerInfo::CountShares() const 76 { 77 return fShareInfos.Count(); 78 } 79 80 // ShareInfoAt 81 ExtendedShareInfo* ShareInfoAt(int32 index) const82ExtendedServerInfo::ShareInfoAt(int32 index) const 83 { 84 if (index < 0 || index >= fShareInfos.Count()) 85 return NULL; 86 return fShareInfos.ElementAt(index); 87 } 88 89 // GetShareInfo 90 ExtendedShareInfo* GetShareInfo(const char * name)91ExtendedServerInfo::GetShareInfo(const char* name) 92 { 93 for (int32 i = 0; ExtendedShareInfo* shareInfo = ShareInfoAt(i); i++) { 94 if (strcmp(shareInfo->GetShareName(), name) == 0) 95 return shareInfo; 96 } 97 98 return NULL; 99 } 100 101 // SetTo 102 status_t SetTo(ServerInfo * serverInfo)103ExtendedServerInfo::SetTo(ServerInfo* serverInfo) 104 { 105 if (!serverInfo) 106 return B_BAD_VALUE; 107 // set name and connection method 108 const char* name = serverInfo->GetServerName(); 109 HashString addressString; 110 if (!name || strlen(name) == 0) { 111 status_t error = fAddress.GetString(&addressString, false); 112 if (error != B_OK) 113 return error; 114 name = addressString.GetString(); 115 } 116 if (!fServerName.SetTo(name) 117 || !fConnectionMethod.SetTo(serverInfo->GetConnectionMethod())) { 118 return B_NO_MEMORY; 119 } 120 // add the shares 121 int32 shareCount = serverInfo->CountShares(); 122 for (int32 i = 0; i < shareCount; i++) { 123 const ShareInfo& shareInfo = serverInfo->ShareInfoAt(i); 124 status_t error = _AddShare(&shareInfo); 125 if (error != B_OK) 126 return error; 127 } 128 return B_OK; 129 } 130 131 // SetState 132 void SetState(uint32 state)133ExtendedServerInfo::SetState(uint32 state) 134 { 135 fState = state; 136 } 137 138 // GetState 139 uint32 GetState() const140ExtendedServerInfo::GetState() const 141 { 142 return fState; 143 } 144 145 // _AddShare 146 status_t _AddShare(const ShareInfo * info)147ExtendedServerInfo::_AddShare(const ShareInfo* info) 148 { 149 ExtendedShareInfo* extendedInfo = new(std::nothrow) ExtendedShareInfo; 150 if (!extendedInfo) 151 return B_NO_MEMORY; 152 status_t error = extendedInfo->SetTo(info); 153 if (error != B_OK) { 154 delete extendedInfo; 155 return error; 156 } 157 error = fShareInfos.PushBack(extendedInfo); 158 if (error != B_OK) { 159 delete extendedInfo; 160 return error; 161 } 162 return B_OK; 163 } 164 165