1 /* 2 * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef PACKAGE_INFO_PRINTER_H 6 #define PACKAGE_INFO_PRINTER_H 7 8 9 #include <stdio.h> 10 11 #include <package/hpkg/PackageInfoAttributeValue.h> 12 #include <package/PackageInfo.h> 13 14 15 using namespace BPackageKit; 16 using BPackageKit::BHPKG::BGlobalWritableFileInfoData; 17 using BPackageKit::BHPKG::BPackageInfoAttributeValue; 18 using BPackageKit::BHPKG::BUserData; 19 using BPackageKit::BHPKG::BUserSettingsFileInfoData; 20 21 22 class PackageInfoPrinter { 23 public: 24 void PrintPackageInfo(const BPackageInfo& info) 25 { 26 PrintName(info.Name()); 27 PrintSummary(info.Summary()); 28 PrintDescription(info.Description()); 29 PrintVendor(info.Vendor()); 30 PrintPackager(info.Packager()); 31 32 if (!info.BasePackage().IsEmpty()) 33 PrintBasePackage(info.BasePackage()); 34 35 PrintFlags(info.Flags()); 36 PrintArchitecture((uint32)info.Architecture()); 37 PrintVersion(info.Version()); 38 39 int32 count = info.CopyrightList().CountStrings(); 40 for (int32 i = 0; i < count; i++) 41 PrintCopyright(info.CopyrightList().StringAt(i)); 42 43 count = info.LicenseList().CountStrings(); 44 for (int32 i = 0; i < count; i++) 45 PrintLicense(info.LicenseList().StringAt(i)); 46 47 count = info.URLList().CountStrings(); 48 for (int32 i = 0; i < count; i++) 49 PrintUrl(info.URLList().StringAt(i)); 50 51 count = info.SourceURLList().CountStrings(); 52 for (int32 i = 0; i < count; i++) 53 PrintSourceUrl(info.SourceURLList().StringAt(i)); 54 55 count = info.ProvidesList().CountItems(); 56 for (int32 i = 0; i < count; i++) 57 PrintProvides(*info.ProvidesList().ItemAt(i)); 58 59 count = info.RequiresList().CountItems(); 60 for (int32 i = 0; i < count; i++) 61 PrintRequires(*info.RequiresList().ItemAt(i)); 62 63 count = info.SupplementsList().CountItems(); 64 for (int32 i = 0; i < count; i++) 65 PrintSupplements(*info.SupplementsList().ItemAt(i)); 66 67 count = info.ConflictsList().CountItems(); 68 for (int32 i = 0; i < count; i++) 69 PrintConflicts(*info.ConflictsList().ItemAt(i)); 70 71 count = info.FreshensList().CountItems(); 72 for (int32 i = 0; i < count; i++) 73 PrintFreshens(*info.FreshensList().ItemAt(i)); 74 75 count = info.ReplacesList().CountStrings(); 76 for (int32 i = 0; i < count; i++) 77 PrintReplaces(info.ReplacesList().StringAt(i)); 78 79 count = info.GlobalWritableFileInfos().CountItems(); 80 for (int32 i = 0; i < count; i++) 81 PrintGlobalWritableFile(*info.GlobalWritableFileInfos().ItemAt(i)); 82 83 count = info.UserSettingsFileInfos().CountItems(); 84 for (int32 i = 0; i < count; i++) 85 PrintUserSettingsFile(*info.UserSettingsFileInfos().ItemAt(i)); 86 87 count = info.Users().CountItems(); 88 for (int32 i = 0; i < count; i++) 89 PrintUser(*info.Users().ItemAt(i)); 90 91 count = info.Groups().CountStrings(); 92 for (int32 i = 0; i < count; i++) 93 PrintGroup(info.Groups().StringAt(i)); 94 95 count = info.PostInstallScripts().CountStrings(); 96 for (int32 i = 0; i < count; i++) 97 PrintPostInstallScript(info.PostInstallScripts().StringAt(i)); 98 99 count = info.PostInstallScripts().CountStrings(); 100 for (int32 i = 0; i < count; i++) 101 PrintPostInstallScript(info.PostInstallScripts().StringAt(i)); 102 103 if (!info.InstallPath().IsEmpty()) 104 PrintInstallPath(info.InstallPath()); 105 } 106 107 bool PrintAttribute(const BPackageInfoAttributeValue& value) 108 { 109 switch (value.attributeID) { 110 case B_PACKAGE_INFO_NAME: 111 PrintName(value.string); 112 break; 113 114 case B_PACKAGE_INFO_SUMMARY: 115 PrintSummary(value.string); 116 break; 117 118 case B_PACKAGE_INFO_DESCRIPTION: 119 PrintDescription(value.string); 120 break; 121 122 case B_PACKAGE_INFO_VENDOR: 123 PrintVendor(value.string); 124 break; 125 126 case B_PACKAGE_INFO_PACKAGER: 127 PrintPackager(value.string); 128 break; 129 130 case B_PACKAGE_INFO_BASE_PACKAGE: 131 PrintBasePackage(value.string); 132 break; 133 134 case B_PACKAGE_INFO_FLAGS: 135 PrintFlags(value.unsignedInt); 136 break; 137 138 case B_PACKAGE_INFO_ARCHITECTURE: 139 PrintArchitecture(value.unsignedInt); 140 break; 141 142 case B_PACKAGE_INFO_VERSION: 143 PrintVersion(value.version); 144 break; 145 146 case B_PACKAGE_INFO_COPYRIGHTS: 147 PrintCopyright(value.string); 148 break; 149 150 case B_PACKAGE_INFO_LICENSES: 151 PrintLicense(value.string); 152 break; 153 154 case B_PACKAGE_INFO_URLS: 155 PrintUrl(value.string); 156 break; 157 158 case B_PACKAGE_INFO_SOURCE_URLS: 159 PrintSourceUrl(value.string); 160 break; 161 162 case B_PACKAGE_INFO_PROVIDES: 163 PrintProvides(value.resolvable); 164 break; 165 166 case B_PACKAGE_INFO_REQUIRES: 167 PrintRequires(value.resolvableExpression); 168 break; 169 170 case B_PACKAGE_INFO_SUPPLEMENTS: 171 PrintSupplements(value.resolvableExpression); 172 break; 173 174 case B_PACKAGE_INFO_CONFLICTS: 175 PrintConflicts(value.resolvableExpression); 176 break; 177 178 case B_PACKAGE_INFO_FRESHENS: 179 PrintFreshens(value.resolvableExpression); 180 break; 181 182 case B_PACKAGE_INFO_REPLACES: 183 PrintReplaces(value.string); 184 break; 185 186 case B_PACKAGE_INFO_GLOBAL_WRITABLE_FILES: 187 PrintGlobalWritableFile(value.globalWritableFileInfo); 188 break; 189 190 case B_PACKAGE_INFO_USER_SETTINGS_FILES: 191 PrintUserSettingsFile(value.userSettingsFileInfo); 192 break; 193 194 case B_PACKAGE_INFO_USERS: 195 PrintUser(value.user); 196 break; 197 198 case B_PACKAGE_INFO_GROUPS: 199 PrintGroup(value.string); 200 break; 201 202 case B_PACKAGE_INFO_POST_INSTALL_SCRIPTS: 203 PrintPostInstallScript(value.string); 204 break; 205 206 case B_PACKAGE_INFO_INSTALL_PATH: 207 PrintInstallPath(value.string); 208 break; 209 210 case B_PACKAGE_INFO_CHECKSUM: 211 PrintChecksum(value.string); 212 break; 213 214 default: 215 printf("\tunknown or future attribute: " 216 "BPackageInfoAttributeID #%d\n", value.attributeID); 217 return true; 218 } 219 220 return true; 221 } 222 223 void PrintName(const char* name) const 224 { 225 printf("\tname: %s\n", name); 226 } 227 228 void PrintSummary(const char* summary) const 229 { 230 printf("\tsummary: %s\n", summary); 231 } 232 233 void PrintDescription(const char* description) const 234 { 235 printf("\tdescription: %s\n", description); 236 } 237 238 void PrintVendor(const char* vendor) const 239 { 240 printf("\tvendor: %s\n", vendor); 241 } 242 243 void PrintPackager(const char* packager) const 244 { 245 printf("\tpackager: %s\n", packager); 246 } 247 248 void PrintBasePackage(const char* basePackage) const 249 { 250 printf("\tbase package: %s\n", basePackage); 251 } 252 253 void PrintFlags(uint32 flags) const 254 { 255 if (flags == 0) 256 return; 257 258 printf("\tflags:\n"); 259 if ((flags & B_PACKAGE_FLAG_APPROVE_LICENSE) != 0) 260 printf("\t\tapprove_license\n"); 261 if ((flags & B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0) 262 printf("\t\tsystem_package\n"); 263 } 264 265 void PrintArchitecture(uint32 architecture) const 266 { 267 printf("\tarchitecture: %s\n", 268 architecture < B_PACKAGE_ARCHITECTURE_ENUM_COUNT 269 ? BPackageInfo::kArchitectureNames[architecture] 270 : "<invalid>"); 271 } 272 273 void PrintVersion(const BPackageVersion& version) const 274 { 275 printf("\tversion: %s\n", version.ToString().String()); 276 } 277 278 void PrintCopyright(const char* copyright) const 279 { 280 printf("\tcopyright: %s\n", copyright); 281 } 282 283 void PrintLicense(const char* license) const 284 { 285 printf("\tlicense: %s\n", license); 286 } 287 288 void PrintUrl(const char* url) const 289 { 290 printf("\tURL: %s\n", url); 291 } 292 293 void PrintSourceUrl(const char* sourceUrl) const 294 { 295 printf("\tsource URL: %s\n", sourceUrl); 296 } 297 298 void PrintProvides(const BPackageResolvable& provides) const 299 { 300 printf("\tprovides: %s", provides.Name().String()); 301 if (provides.Version().InitCheck() == B_OK) 302 printf(" = %s", provides.Version().ToString().String()); 303 304 if (provides.CompatibleVersion().InitCheck() == B_OK) { 305 printf(" (compatible >= %s)", 306 provides.CompatibleVersion().ToString().String()); 307 } 308 printf("\n"); 309 } 310 311 void PrintRequires(const BPackageResolvableExpression& requires) const 312 { 313 _PrintResolvableExpression("requires", requires); 314 } 315 316 void PrintSupplements(const BPackageResolvableExpression& supplements) 317 const 318 { 319 _PrintResolvableExpression("supplements", supplements); 320 } 321 322 void PrintConflicts(const BPackageResolvableExpression& conflicts) const 323 { 324 _PrintResolvableExpression("conflicts", conflicts); 325 } 326 327 void PrintFreshens(const BPackageResolvableExpression& freshens) const 328 { 329 _PrintResolvableExpression("freshens", freshens); 330 } 331 332 void PrintReplaces(const char* replaces) const 333 { 334 printf("\treplaces: %s\n", replaces); 335 } 336 337 void PrintGlobalWritableFile(const BGlobalWritableFileInfo& info) const 338 { 339 printf("\tglobal writable file: %s", info.Path().String()); 340 if (info.IsDirectory()) 341 printf( " directory"); 342 if (info.UpdateType() < B_WRITABLE_FILE_UPDATE_TYPE_ENUM_COUNT) { 343 printf(" %s\n", 344 BPackageInfo::kWritableFileUpdateTypes[info.UpdateType()]); 345 } else 346 printf("\n"); 347 } 348 349 void PrintUserSettingsFile(const BUserSettingsFileInfo& info) const 350 { 351 printf("\tuser settings file: %s", info.Path().String()); 352 if (info.IsDirectory()) 353 printf( " directory\n"); 354 else if (!info.TemplatePath().IsEmpty()) 355 printf(" template %s\n", info.TemplatePath().String()); 356 else 357 printf("\n"); 358 } 359 360 void PrintUser(const BUser& user) const 361 { 362 printf("\tuser: %s\n", user.Name().String()); 363 if (!user.RealName().IsEmpty()) 364 printf("\t\treal name: %s\n", user.RealName().String()); 365 if (!user.Home().IsEmpty()) 366 printf("\t\thome: %s\n", user.Home().String()); 367 if (!user.Shell().IsEmpty()) 368 printf("\t\tshell: %s\n", user.Shell().String()); 369 370 int32 groupCount = user.Groups().CountStrings(); 371 for (int32 i = 0; i < groupCount; i++) 372 printf("\t\tgroup: %s\n", user.Groups().StringAt(i).String()); 373 } 374 375 void PrintGroup(const char* group) const 376 { 377 printf("\tgroup: %s\n", group); 378 } 379 380 void PrintPostInstallScript(const char* script) const 381 { 382 printf("\tpost install script: %s\n", script); 383 } 384 385 void PrintInstallPath(const char* path) const 386 { 387 printf("\tinstall path: %s\n", path); 388 } 389 390 void PrintChecksum(const char* checksum) const 391 { 392 printf("\tchecksum: %s\n", checksum); 393 } 394 395 private: 396 void _PrintResolvableExpression(const char* fieldName, 397 const BPackageResolvableExpression& expression) const 398 { 399 printf("\t%s: %s\n", fieldName, expression.ToString().String()); 400 } 401 }; 402 403 404 #endif // PACKAGE_INFO_PRINTER_H 405