PageRenderTime 15ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/Core/KSClientActives.m

http://macfuse.googlecode.com/
Objective C | 144 lines | 90 code | 27 blank | 27 comment | 17 complexity | e3898173adf11d578650507d63e1043f MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. // Copyright 2010 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "KSClientActives.h"
  15. // Dictionary keys for the productInfo_ ivar.
  16. #define kLastRollCallPingKey @"RollCallPing" // NSDate
  17. #define kLastActiveKey @"LastActive" // NSDate
  18. #define kLastActivePingKey @"LastActivePing" // NSDate
  19. #define kSentActiveKey @"SentActive" // NSNumber-wrapped BOOL
  20. #define kSentRollCallKey @"SentRollCall" // NSNumber-wrapped BOOL
  21. // Approximate number of seconds in a day.
  22. #define DAY_SECONDS (24 * 60 * 60)
  23. @implementation KSClientActives
  24. - (id)init {
  25. if ((self = [super init])) {
  26. productInfo_ = [[NSMutableDictionary alloc] init];
  27. }
  28. return self;
  29. }
  30. - (void)dealloc {
  31. [productInfo_ release];
  32. [super dealloc];
  33. }
  34. // Returns a dictionary for a given product ID. If there is no dictionary
  35. // for this product, create a new one.
  36. - (NSMutableDictionary *)infoForProductID:(NSString *)productID {
  37. NSMutableDictionary *info = [productInfo_ objectForKey:productID];
  38. if (info == nil) {
  39. info = [NSMutableDictionary dictionary];
  40. [productInfo_ setObject:info forKey:productID];
  41. }
  42. return info;
  43. }
  44. - (void)setLastRollCallPing:(NSDate *)lastRollCall
  45. lastActivePing:(NSDate *)lastActivePing
  46. lastActive:(NSDate *)lastActive
  47. forProductID:(NSString *)productID {
  48. NSMutableDictionary *info = [self infoForProductID:productID];
  49. if (lastRollCall) [info setObject:lastRollCall forKey:kLastRollCallPingKey];
  50. if (lastActivePing) [info setObject:lastActivePing forKey:kLastActivePingKey];
  51. if (lastActive) [info setObject:lastActive forKey:kLastActiveKey];
  52. }
  53. // Calculates how many active days to report betwee now and the given date.
  54. - (int)activeDaysSinceDate:(NSDate *)date {
  55. if (date == nil) return kKSClientActivesFirstReport;
  56. NSTimeInterval interval = [date timeIntervalSinceNow];
  57. // The interval should be a negative value. Punt if the date is in
  58. // the future.
  59. if (interval > 0) return kKSClientActivesDontReport;
  60. // Flip the sign and figure the number of days.
  61. int days = fabs(interval) / DAY_SECONDS;
  62. if (days == 0) return kKSClientActivesDontReport;
  63. else return days;
  64. }
  65. - (int)rollCallDaysForProductID:(NSString *)productID {
  66. NSMutableDictionary *info = [self infoForProductID:productID];
  67. NSDate *lastRollCallPing = [info objectForKey:kLastRollCallPingKey];
  68. int result = [self activeDaysSinceDate:lastRollCallPing];
  69. return result;
  70. }
  71. - (int)activeDaysForProductID:(NSString *)productID {
  72. NSMutableDictionary *info = [self infoForProductID:productID];
  73. NSDate *lastActivePing = [info objectForKey:kLastActivePingKey];
  74. NSDate *lastActive = [info objectForKey:kLastActiveKey];
  75. int result = kKSClientActivesDontReport;
  76. // Only report actives if there's actually been an active.
  77. if (lastActive) {
  78. // Product *has* been active, but we've never reported it.
  79. // So this is the first time.
  80. if (lastActivePing == nil) {
  81. result = kKSClientActivesFirstReport;
  82. } else {
  83. // Otherwise, if the last active is newer than the last active
  84. // ping, then the user has used the product since we last
  85. // reported it.
  86. if ([lastActive timeIntervalSinceDate:lastActivePing] > 0) {
  87. result = [self activeDaysSinceDate:lastActivePing];
  88. }
  89. }
  90. }
  91. return result;
  92. }
  93. - (void)sentRollCallForProductID:(NSString *)productID {
  94. NSMutableDictionary *info = [self infoForProductID:productID];
  95. [info setObject:[NSNumber numberWithBool:YES] forKey:kSentRollCallKey];
  96. }
  97. - (BOOL)didSendRollCallForProductID:(NSString *)productID {
  98. NSMutableDictionary *info = [self infoForProductID:productID];
  99. NSNumber *didSend = [info objectForKey:kSentRollCallKey];
  100. return [didSend boolValue];
  101. }
  102. - (void)sentActiveForProductID:(NSString *)productID {
  103. NSMutableDictionary *info = [self infoForProductID:productID];
  104. [info setObject:[NSNumber numberWithBool:YES] forKey:kSentActiveKey];
  105. }
  106. - (BOOL)didSendActiveForProductID:(NSString *)productID {
  107. NSMutableDictionary *info = [self infoForProductID:productID];
  108. NSNumber *didSend = [info objectForKey:kSentActiveKey];
  109. return [didSend boolValue];
  110. }
  111. - (NSString *)description {
  112. NSString *description =
  113. [NSString stringWithFormat:@"<%@:%p\n"
  114. "\t productInfo=%@\n>", [self class], self, productInfo_];
  115. return description;
  116. }
  117. @end