1 /* 2 * Copyright 2016 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT license 4 * 5 * Authors: 6 * Alexander von Gluck IV <kallisti5@unixzen.com> 7 */ 8 9 10 #include "SoftwareUpdaterWindow.h" 11 12 #include <stdio.h> 13 #include <AppDefs.h> 14 #include <Application.h> 15 #include <GroupLayout.h> 16 #include <GroupLayoutBuilder.h> 17 #include <NodeInfo.h> 18 #include <LayoutBuilder.h> 19 #include <Message.h> 20 #include <package/Context.h> 21 #include <package/RefreshRepositoryRequest.h> 22 #include <package/PackageRoster.h> 23 #include <Resources.h> 24 #include <SeparatorView.h> 25 #include <String.h> 26 27 28 #undef B_TRANSLATION_CONTEXT 29 #define B_TRANSLATION_CONTEXT "SoftwareUpdater" 30 31 32 const uint32 kMsgUpdate = 'iUPD'; 33 const uint32 kMsgExit = 'iEXT'; 34 35 36 using namespace BPackageKit; 37 38 39 SoftwareUpdaterWindow::SoftwareUpdaterWindow() 40 : 41 BWindow(BRect(0, 0, 0, 300), "Software Update", 42 B_TITLED_WINDOW, B_AUTO_UPDATE_SIZE_LIMITS | B_NOT_ZOOMABLE), 43 // fUpdateManager(NULL), 44 fStripeView(NULL), 45 fHeaderView(NULL), 46 fDetailView(NULL), 47 fUpdateButton(NULL), 48 fCancelButton(NULL) 49 { 50 fUpdateManager = new UpdateManager( 51 BPackageKit::B_PACKAGE_INSTALLATION_LOCATION_HOME); 52 fUpdateManager->SetDebugLevel(10); 53 fUpdateManager->Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES 54 | BPackageManager::B_ADD_REMOTE_REPOSITORIES); 55 56 BBitmap* icon = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32); 57 team_info teamInfo; 58 get_team_info(B_CURRENT_TEAM, &teamInfo); 59 app_info appInfo; 60 be_roster->GetRunningAppInfo(teamInfo.team, &appInfo); 61 BNodeInfo::GetTrackerIcon(&appInfo.ref, icon, B_LARGE_ICON); 62 63 fStripeView = new StripeView(icon); 64 65 BButton* fUpdateButton = new BButton("Update now", 66 new BMessage(kMsgUpdate)); 67 fUpdateButton->Hide(); 68 69 BButton* fCancelButton = new BButton("Cancel", 70 new BMessage(kMsgExit)); 71 72 fHeaderView = new BStringView("header", 73 "Checking for updates...", B_WILL_DRAW); 74 fDetailView = new BStringView("detail", "Contacting software repositories" 75 " to check for package updates.", B_WILL_DRAW); 76 77 BFont font; 78 fHeaderView->GetFont(&font); 79 font.SetFace(B_BOLD_FACE); 80 font.SetSize(font.Size() * 1.5); 81 fHeaderView->SetFont(&font, B_FONT_FAMILY_AND_STYLE | B_FONT_SIZE 82 | B_FONT_FLAGS); 83 84 BLayoutBuilder::Group<>(this, B_HORIZONTAL, 0) 85 .Add(fStripeView) 86 .AddGroup(B_VERTICAL, B_USE_SMALL_SPACING) 87 .SetInsets(0, B_USE_DEFAULT_SPACING, 88 B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING) 89 .Add(fHeaderView) 90 .Add(fDetailView) 91 .AddStrut(B_USE_DEFAULT_SPACING) 92 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) 93 .AddGlue() 94 .Add(fCancelButton) 95 .Add(fUpdateButton) 96 .End() 97 .End() 98 .AddGlue() 99 //.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER)) 100 ; 101 CenterOnScreen(); 102 Show(); 103 104 // Refresh our repos and update UI 105 _Refresh(); 106 } 107 108 109 SoftwareUpdaterWindow::~SoftwareUpdaterWindow() 110 { 111 } 112 113 114 bool 115 SoftwareUpdaterWindow::QuitRequested() 116 { 117 be_app->PostMessage(B_QUIT_REQUESTED); 118 return true; 119 } 120 121 122 void 123 SoftwareUpdaterWindow::MessageReceived(BMessage* message) 124 { 125 switch (message->what) { 126 case kMsgExit: 127 QuitRequested(); 128 break; 129 case kMsgUpdate: 130 break; 131 default: 132 BWindow::MessageReceived(message); 133 } 134 } 135 136 137 void 138 SoftwareUpdaterWindow::_Error(const char* error) 139 { 140 fHeaderView->SetText("Error encountered!"); 141 fDetailView->SetText(error); 142 fUpdateButton->Hide(); 143 fCancelButton->Show(); 144 } 145 146 void 147 SoftwareUpdaterWindow::_Refresh() 148 { 149 BPackageRoster roster; 150 BStringList repoNames(20); 151 152 status_t result = roster.GetRepositoryNames(repoNames); 153 if (result != B_OK) { 154 _Error("Unable to obtain repository names."); 155 return; 156 } 157 158 for (int i = 0; i < repoNames.CountStrings(); i++) { 159 const BString& repoName = repoNames.StringAt(i); 160 BRepositoryConfig repoConfig; 161 result = roster.GetRepositoryConfig(repoName, &repoConfig); 162 if (result != B_OK) { 163 printf("Skipping '%s' repo due to unknown config\n", 164 repoName.String()); 165 continue; 166 } 167 /* 168 BRefreshRepositoryRequest request(context, repoConfig); 169 result = request.Process(); 170 if (result != B_OK) { 171 printf("Skipping %s repo due to unreachable repo"); 172 continue; 173 } 174 */ 175 } 176 } 177