ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

macosx-updates.mm

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Disable symbol overrides so that we can use system headers.
00024 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00025 
00026 #include "common/system.h"
00027 #include "backends/updates/macosx/macosx-updates.h"
00028 
00029 #ifdef USE_SPARKLE
00030 #include "common/translation.h"
00031 #include "common/config-manager.h"
00032 
00033 #include <Cocoa/Cocoa.h>
00034 #include <Sparkle/Sparkle.h>
00035 
00036 #include <AvailabilityMacros.h>
00037 
00038 SUUpdater *sparkleUpdater;
00039 
00051 MacOSXUpdateManager::MacOSXUpdateManager() {
00052     NSBundle* mainBundle = [NSBundle mainBundle];
00053 
00054     NSString *version = [mainBundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey];
00055     if (!version || [version isEqualToString:@""]) {
00056         warning("Running not in bundle, skipping Sparkle initialization");
00057 
00058         sparkleUpdater = nullptr;
00059         return;
00060     }
00061 
00062     NSMenuItem *menuItem = [[NSApp mainMenu] itemAtIndex:0];
00063     NSMenu *applicationMenu = [menuItem submenu];
00064 
00065     // Init Sparkle
00066     sparkleUpdater = [SUUpdater sharedUpdater];
00067 
00068     NSString* feedbackURL = [mainBundle objectForInfoDictionaryKey:@"SUFeedURL"];
00069 
00070     // Set appcast URL
00071     [sparkleUpdater setFeedURL:[NSURL URLWithString:feedbackURL]];
00072 
00073     // Get current encoding
00074     CFStringRef encStr = CFStringCreateWithCString(NULL, TransMan.getCurrentCharset().c_str(), kCFStringEncodingASCII);
00075     CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding(encStr);
00076     CFRelease(encStr);
00077 
00078     // Add "Check for Updates..." menu item
00079     CFStringRef title = CFStringCreateWithCString(NULL, _("Check for Updates..."), stringEncoding);
00080     NSMenuItem *updateMenuItem = [applicationMenu insertItemWithTitle:(NSString *)title action:@selector(checkForUpdates:) keyEquivalent:@"" atIndex:1];
00081     CFRelease(title);
00082 
00083     // Set the target of the new menu item
00084     [updateMenuItem setTarget:sparkleUpdater];
00085 
00086     // Finally give up our references to the objects
00087     [menuItem release];
00088 
00089     if (!ConfMan.hasKey("updates_check")
00090             || ConfMan.getInt("updates_check") == Common::UpdateManager::kUpdateIntervalNotSupported) {
00091         setAutomaticallyChecksForUpdates(kUpdateStateDisabled);
00092     } else {
00093         setAutomaticallyChecksForUpdates(kUpdateStateEnabled);
00094         setUpdateCheckInterval(normalizeInterval(ConfMan.getInt("updates_check")));
00095     }
00096 }
00097 
00098 MacOSXUpdateManager::~MacOSXUpdateManager() {
00099     [sparkleUpdater release];
00100 }
00101 
00102 void MacOSXUpdateManager::checkForUpdates() {
00103     if (sparkleUpdater == nullptr)
00104         return;
00105 
00106     [sparkleUpdater checkForUpdates:nil];
00107 }
00108 
00109 void MacOSXUpdateManager::setAutomaticallyChecksForUpdates(UpdateManager::UpdateState state) {
00110     if (state == kUpdateStateNotSupported)
00111         return;
00112 
00113     if (sparkleUpdater == nullptr)
00114         return;
00115 
00116     [sparkleUpdater setAutomaticallyChecksForUpdates:(state == kUpdateStateEnabled ? YES : NO)];
00117 }
00118 
00119 Common::UpdateManager::UpdateState MacOSXUpdateManager::getAutomaticallyChecksForUpdates() {
00120     if (sparkleUpdater == nullptr)
00121         return kUpdateStateDisabled;
00122 
00123     if ([sparkleUpdater automaticallyChecksForUpdates])
00124         return kUpdateStateEnabled;
00125     else
00126         return kUpdateStateDisabled;
00127 }
00128 
00129 void MacOSXUpdateManager::setUpdateCheckInterval(int interval) {
00130     if (sparkleUpdater == nullptr)
00131         return;
00132 
00133     if (interval == kUpdateIntervalNotSupported)
00134         return;
00135 
00136     interval = normalizeInterval(interval);
00137 
00138     [sparkleUpdater setUpdateCheckInterval:(NSTimeInterval)interval];
00139 }
00140 
00141 int MacOSXUpdateManager::getUpdateCheckInterval() {
00142     if (sparkleUpdater == nullptr)
00143         return kUpdateIntervalOneDay;
00144 
00145     // This is kind of a hack but necessary, as the value stored by Sparkle
00146     // might have been changed outside of ScummVM (in which case we return the
00147     // default interval of one day)
00148 
00149     UpdateInterval updateInterval = (UpdateInterval)[sparkleUpdater updateCheckInterval];
00150     switch (updateInterval) {
00151     case kUpdateIntervalOneDay:
00152     case kUpdateIntervalOneWeek:
00153     case kUpdateIntervalOneMonth:
00154         return updateInterval;
00155 
00156     default:
00157         // Return the default value (one day)
00158         return kUpdateIntervalOneDay;
00159     }
00160 }
00161 
00162 bool MacOSXUpdateManager::getLastUpdateCheckTimeAndDate(TimeDate &t) {
00163     if (sparkleUpdater == nullptr)
00164         return false;
00165 
00166     NSDate *date = [sparkleUpdater lastUpdateCheckDate];
00167 #ifdef MAC_OS_X_VERSION_10_10
00168     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
00169     NSDateComponents *components = [gregorian components:(NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:date];
00170 #else
00171     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
00172     NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
00173 #endif
00174 
00175     t.tm_wday = [components weekday];
00176     t.tm_year = [components year];
00177     t.tm_mon = [components month];
00178     t.tm_mday = [components day];
00179     t.tm_hour = [components hour];
00180     t.tm_min = [components minute];
00181     t.tm_sec = [components second];
00182 
00183     [gregorian release];
00184 
00185     return true;
00186 }
00187 
00188 #endif


Generated on Sat May 18 2019 05:01:08 for ResidualVM by doxygen 1.7.1
curved edge   curved edge