Skip to content

Commit 94ffaef

Browse files
committed
improved scan compare and localizations
fix for #45
1 parent 22a1d94 commit 94ffaef

File tree

6 files changed

+230
-325
lines changed

6 files changed

+230
-325
lines changed

AppDelegate.m

Lines changed: 31 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// KnockKnock
44
//
55

6+
#import "diff.h"
67
#import "consts.h"
78
#import "Update.h"
89
#import "utilities.h"
@@ -1337,234 +1338,59 @@ -(IBAction)showPreferences:(id)sender
13371338
//compare a past to a current scan
13381339
-(IBAction)compareScans:(id)sender
13391340
{
1340-
//error
1341-
NSError* error = nil;
1342-
1343-
//flag
1344-
BOOL errors = NO;
1345-
1346-
//alert
1347-
NSAlert* alert = nil;
1348-
13491341
//previous scan
13501342
NSString* prevScan = nil;
13511343
NSDictionary* prevScanContents = nil;
1352-
1353-
//added items
1354-
NSMutableArray* addedItems = nil;
1355-
1356-
//removed items
1357-
NSMutableArray* removedItems = nil;
1344+
NSDictionary* currentScanContents = nil;
13581345

13591346
//diff results
1360-
NSMutableString* differences = nil;
1361-
1362-
//'browse' panel
1363-
NSOpenPanel *panel = nil;
1364-
1365-
//init
1366-
differences = [NSMutableString string];
1347+
NSString* differences = nil;
13671348

13681349
//init panel
1369-
panel = [NSOpenPanel openPanel];
1350+
NSOpenPanel* panel = [NSOpenPanel openPanel];
13701351

1371-
//allow files
1352+
//configure panel
13721353
panel.canChooseFiles = YES;
1373-
1374-
//disallow directories
13751354
panel.canChooseDirectories = NO;
1376-
1377-
//disable multiple selections
13781355
panel.allowsMultipleSelection = NO;
1379-
1380-
//can open app bundles
1356+
panel.allowedFileTypes = @[@"json"];
13811357
panel.treatsFilePackagesAsDirectories = YES;
13821358

13831359
//default to desktop
1384-
// as this where scans are suggested to be saved
1385-
panel.directoryURL = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains (NSDesktopDirectory, NSUserDomainMask, YES) firstObject]];
1386-
1387-
//show panel
1388-
// but bail on cancel
1389-
if(NSModalResponseCancel == [panel runModal])
1390-
{
1391-
//bail
1392-
goto bail;
1393-
}
1394-
1395-
//load previous scan
1396-
prevScan = [NSString stringWithContentsOfURL:panel.URL encoding:NSUTF8StringEncoding error:&error];
1397-
if(nil == prevScan)
1398-
{
1399-
//set
1400-
errors = YES;
1401-
goto bail;
1402-
}
1403-
1404-
//serialize json
1405-
prevScanContents = [NSJSONSerialization JSONObjectWithData:[prevScan dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
1406-
if(YES != [prevScanContents isKindOfClass:[NSDictionary class]])
1407-
{
1408-
//set
1409-
errors = YES;
1410-
goto bail;
1411-
}
1360+
panel.directoryURL = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject]];
14121361

1413-
//init
1414-
addedItems = [NSMutableArray array];
1415-
removedItems = [NSMutableArray array];
1416-
1417-
//compare
1418-
// for now, only adds / removes
1419-
for(PluginBase* plugin in self.plugins)
1420-
{
1421-
//vars
1422-
NSArray* prevItems = nil;
1423-
NSArray* currentItems = nil;
1424-
1425-
NSSet *prevPaths = nil;
1426-
NSSet *currentPaths = nil;
1362+
//show panel, bail on cancel
1363+
if([panel runModal] == NSModalResponseOK) {
14271364

1428-
NSMutableSet *addedPaths = nil;
1429-
NSMutableSet *removedPaths = nil;
1365+
//load previous scan
1366+
prevScan = [NSString stringWithContentsOfURL:panel.URL encoding:NSUTF8StringEncoding error:nil];
14301367

1431-
NSMutableDictionary* addedItem = nil;
1432-
NSMutableDictionary* removedItem = nil;
1433-
1434-
NSString* key = plugin.name;
1435-
1436-
//trusted items?
1437-
if(YES == self.prefsWindowController.showTrustedItems)
1438-
{
1439-
//set
1440-
currentItems = plugin.allItems;
1441-
}
1442-
//set items
1443-
// just unknown items
1444-
else
1445-
{
1446-
//set
1447-
currentItems = plugin.untrustedItems;
1448-
}
1368+
//parse previous scan JSON
1369+
prevScanContents = [NSJSONSerialization JSONObjectWithData:[prevScan dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
14491370

1450-
prevItems = prevScanContents[key];
1371+
//parse current scan JSON
1372+
currentScanContents = [NSJSONSerialization JSONObjectWithData:[[self scanToJSON] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
14511373

1452-
prevPaths = [NSSet setWithArray:[prevItems valueForKey:@"path"]];
1453-
currentPaths = [NSSet setWithArray:[currentItems valueForKey:@"path"]];
1454-
1455-
addedPaths = [currentPaths mutableCopy];
1456-
[addedPaths minusSet:prevPaths];
1457-
1458-
removedPaths = [prevPaths mutableCopy];
1459-
[removedPaths minusSet:currentPaths];
1460-
1461-
//save added items
1462-
for(ItemBase* currentItem in currentItems)
1463-
{
1464-
//added?
1465-
if(YES != [addedPaths containsObject:currentItem.path])
1466-
{
1467-
//skip
1468-
continue;
1469-
}
1470-
1471-
//convert to dictionary
1472-
addedItem = [[NSJSONSerialization JSONObjectWithData:[[NSString stringWithFormat:@"%@", [currentItem toJSON]] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] mutableCopy];
1473-
if(nil == addedItem)
1474-
{
1475-
//skip
1476-
continue;
1477-
}
1478-
1479-
//add key
1480-
addedItem[@"key"] = key;
1481-
1482-
//add
1483-
[addedItems addObject:addedItem];
1484-
}
1485-
1486-
//save removed items
1487-
for(NSDictionary* prevItem in prevItems)
1488-
{
1489-
//removed?
1490-
if(YES != [removedPaths containsObject:prevItem[@"path"]])
1491-
{
1492-
//skip
1493-
continue;
1494-
}
1374+
//diff scans
1375+
differences = diffScans(prevScanContents, currentScanContents);
1376+
if(differences) {
1377+
1378+
//show diff window
1379+
self.diffWindowController = [[DiffWindowController alloc] initWithWindowNibName:@"DiffWindow"];
1380+
self.diffWindowController.differences = differences;
1381+
[self.diffWindowController showWindow:self];
14951382

1496-
//make copy
1497-
removedItem = [prevItem mutableCopy];
1498-
removedItem[@"key"] = key;
1499-
1500-
//add
1501-
[removedItems addObject:removedItem];
1502-
}
1503-
}
1504-
1505-
//any changes
1506-
if( (0 == addedItems.count) &&
1507-
(0 == removedItems.count) )
1508-
{
1509-
//no changes
1510-
differences = [NSLocalizedString(@"No Changes Detected", @"No Changes Detected") mutableCopy];
1511-
}
1512-
1513-
//any added items?
1514-
if(0 != addedItems.count)
1515-
{
1516-
//msg
1517-
[differences appendString:NSLocalizedString(@"NEW ITEMS:\r\n", @"NEW ITEMS:\r\n")];
1518-
1519-
//add each item
1520-
for(NSDictionary* item in addedItems)
1521-
{
1522-
//add
1523-
[differences appendString:[NSString stringWithFormat:@"(%@): %@\r\n", [item[@"key"] substringToIndex:[item[@"key"] length]-1], item.description]];
15241383
}
1525-
}
1526-
1527-
//any removed items
1528-
if(0 != removedItems.count)
1529-
{
1530-
//msg
1531-
[differences appendString:NSLocalizedString(@"REMOVED ITEMS:\r\n", @"REMOVED ITEMS:\r\n")];
1532-
1533-
//add each item
1534-
for(NSDictionary* item in removedItems)
1535-
{
1536-
//add
1537-
[differences appendString:[NSString stringWithFormat:@"(%@): %@\r\n", [item[@"key"] substringToIndex:[item[@"key"] length]-1], item.description]];
1384+
//error
1385+
else {
1386+
1387+
//show error alert
1388+
NSAlert* alert = [[NSAlert alloc] init];
1389+
[alert addButtonWithTitle:@"Ok"];
1390+
alert.messageText = NSLocalizedString(@"ERROR: Failed to compare scans", @"ERROR: Failed to compare scans");
1391+
[alert runModal];
15381392
}
15391393
}
1540-
1541-
//alloc window controller
1542-
self.diffWindowController = [[DiffWindowController alloc] initWithWindowNibName:@"DiffWindow"];
1543-
1544-
//set text (differences)
1545-
self.diffWindowController.differences = differences;
1546-
1547-
//show window
1548-
[self.diffWindowController showWindow:self];
1549-
1550-
bail:
1551-
1552-
//any errors?
1553-
// show alert
1554-
if(YES == errors)
1555-
{
1556-
//init alert
1557-
alert = [[NSAlert alloc] init];
1558-
1559-
//ok
1560-
[alert addButtonWithTitle:@"Ok"];
1561-
1562-
//error msg
1563-
alert.messageText = NSLocalizedString(@"ERROR: Failed to compare scans", @"ERROR: Failed to compare scans");
1564-
1565-
//show popup
1566-
[alert runModal];
1567-
}
15681394

15691395
return;
15701396
}

DiffWindowController.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@
1313
//differences
1414
@property(nonatomic, retain)NSString* differences;
1515

16-
//(path to) plist
17-
//@property(nonatomic, retain)NSString* plist;
18-
19-
//signing info
20-
//@property(nonatomic, retain)NSDictionary* signingInfo;
21-
2216
//plist contents
2317
@property (unsafe_unretained) IBOutlet NSTextView *contents;
2418

25-
//plist path
26-
//@property (weak) IBOutlet NSTextField *path;
27-
2819
@end

KnockKnock.xcodeproj/project.pbxproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
CDBE491A1B5B25E30031FC22 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDBE49191B5B25E30031FC22 /* SystemConfiguration.framework */; };
120120
CDBE491F1B5B46040031FC22 /* logInOutIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = CDBE491E1B5B46040031FC22 /* logInOutIcon.png */; };
121121
CDBFE39A2A49B8BA005A9819 /* MachO.m in Sources */ = {isa = PBXBuildFile; fileRef = CDBFE3972A49B8B9005A9819 /* MachO.m */; };
122+
CDC988162EF101A700F5C653 /* diff.m in Sources */ = {isa = PBXBuildFile; fileRef = CDC988152EF101A500F5C653 /* diff.m */; };
122123
CDCA2C212A49B71500E8DD57 /* libDumpBTM.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CDCA2C202A49B71500E8DD57 /* libDumpBTM.a */; };
123124
CDD83FD61B50C76F0037124E /* cronIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = CDD83FD51B50C76F0037124E /* cronIcon.png */; };
124125
CDDBC2301B04771100B021E0 /* ServiceManagement.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDDBC22F1B04771100B021E0 /* ServiceManagement.framework */; };
@@ -335,6 +336,8 @@
335336
CDBFE3952A49B8B9005A9819 /* dumpBTM.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dumpBTM.h; sourceTree = "<group>"; };
336337
CDBFE3972A49B8B9005A9819 /* MachO.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MachO.m; sourceTree = "<group>"; };
337338
CDBFE3982A49B8B9005A9819 /* MachO.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MachO.h; sourceTree = "<group>"; };
339+
CDC988142EF1019D00F5C653 /* diff.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = diff.h; sourceTree = "<group>"; };
340+
CDC988152EF101A500F5C653 /* diff.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = diff.m; sourceTree = "<group>"; };
338341
CDCA2C202A49B71500E8DD57 /* libDumpBTM.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libDumpBTM.a; path = Libraries/BTM/libDumpBTM.a; sourceTree = "<group>"; };
339342
CDD83FD51B50C76F0037124E /* cronIcon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = cronIcon.png; path = images/cronIcon.png; sourceTree = SOURCE_ROOT; };
340343
CDDBC22F1B04771100B021E0 /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
@@ -448,6 +451,8 @@
448451
CD585493219FE61D00A438B0 /* Assets.xcassets */,
449452
CDA81D481A95B492009790E2 /* consts.h */,
450453
CDE241A92C8556A40099F82B /* DiffWindowController.h */,
454+
CDC988142EF1019D00F5C653 /* diff.h */,
455+
CDC988152EF101A500F5C653 /* diff.m */,
451456
CDE241AA2C8556A40099F82B /* DiffWindowController.m */,
452457
CD6095501A8329FA00E091CD /* images */,
453458
CD6FD7AB2C7AAD87001C59F2 /* MainMenu.xib */,
@@ -873,6 +878,7 @@
873878
CDADCFA52EE7833C00C6DBF4 /* LogInOutHooks.m in Sources */,
874879
CDADCFA62EE7833C00C6DBF4 /* LoginItems.m in Sources */,
875880
CDADCFA72EE7833C00C6DBF4 /* QuicklookPlugins.m in Sources */,
881+
CDC988162EF101A700F5C653 /* diff.m in Sources */,
876882
CDADCFA82EE7833C00C6DBF4 /* AuthorizationPlugins.m in Sources */,
877883
CDADCFA92EE7833C00C6DBF4 /* Kexts.m in Sources */,
878884
CDADCFAA2EE7833C00C6DBF4 /* DylibInserts.m in Sources */,
@@ -1135,7 +1141,7 @@
11351141
CODE_SIGN_IDENTITY = "Developer ID Application";
11361142
CODE_SIGN_STYLE = Manual;
11371143
COMBINE_HIDPI_IMAGES = YES;
1138-
CURRENT_PROJECT_VERSION = 4.0.1;
1144+
CURRENT_PROJECT_VERSION = 4.0.2;
11391145
DEVELOPMENT_TEAM = VBG97UB4TA;
11401146
ENABLE_HARDENED_RUNTIME = YES;
11411147
GCC_PRECOMPILE_PREFIX_HEADER = YES;
@@ -1150,7 +1156,7 @@
11501156
"$(PROJECT_DIR)/Libraries/BTM",
11511157
);
11521158
MACOSX_DEPLOYMENT_TARGET = 10.15;
1153-
MARKETING_VERSION = 4.0.1;
1159+
MARKETING_VERSION = 4.0.2;
11541160
ONLY_ACTIVE_ARCH = NO;
11551161
PRODUCT_BUNDLE_IDENTIFIER = "com.objective-see.${PRODUCT_NAME:rfc1034identifier}";
11561162
PRODUCT_NAME = KnockKnock;
@@ -1167,7 +1173,7 @@
11671173
CODE_SIGN_IDENTITY = "Developer ID Application";
11681174
CODE_SIGN_STYLE = Manual;
11691175
COMBINE_HIDPI_IMAGES = YES;
1170-
CURRENT_PROJECT_VERSION = 4.0.1;
1176+
CURRENT_PROJECT_VERSION = 4.0.2;
11711177
DEVELOPMENT_TEAM = VBG97UB4TA;
11721178
ENABLE_HARDENED_RUNTIME = YES;
11731179
GCC_PRECOMPILE_PREFIX_HEADER = YES;
@@ -1182,7 +1188,7 @@
11821188
"$(PROJECT_DIR)/Libraries/BTM",
11831189
);
11841190
MACOSX_DEPLOYMENT_TARGET = 10.15;
1185-
MARKETING_VERSION = 4.0.1;
1191+
MARKETING_VERSION = 4.0.2;
11861192
ONLY_ACTIVE_ARCH = NO;
11871193
PRODUCT_BUNDLE_IDENTIFIER = "com.objective-see.${PRODUCT_NAME:rfc1034identifier}";
11881194
PRODUCT_NAME = KnockKnock;

0 commit comments

Comments
 (0)