|
3 | 3 | // KnockKnock |
4 | 4 | // |
5 | 5 |
|
| 6 | +#import "diff.h" |
6 | 7 | #import "consts.h" |
7 | 8 | #import "Update.h" |
8 | 9 | #import "utilities.h" |
@@ -1337,234 +1338,59 @@ -(IBAction)showPreferences:(id)sender |
1337 | 1338 | //compare a past to a current scan |
1338 | 1339 | -(IBAction)compareScans:(id)sender |
1339 | 1340 | { |
1340 | | - //error |
1341 | | - NSError* error = nil; |
1342 | | - |
1343 | | - //flag |
1344 | | - BOOL errors = NO; |
1345 | | - |
1346 | | - //alert |
1347 | | - NSAlert* alert = nil; |
1348 | | - |
1349 | 1341 | //previous scan |
1350 | 1342 | NSString* prevScan = nil; |
1351 | 1343 | NSDictionary* prevScanContents = nil; |
1352 | | - |
1353 | | - //added items |
1354 | | - NSMutableArray* addedItems = nil; |
1355 | | - |
1356 | | - //removed items |
1357 | | - NSMutableArray* removedItems = nil; |
| 1344 | + NSDictionary* currentScanContents = nil; |
1358 | 1345 |
|
1359 | 1346 | //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; |
1367 | 1348 |
|
1368 | 1349 | //init panel |
1369 | | - panel = [NSOpenPanel openPanel]; |
| 1350 | + NSOpenPanel* panel = [NSOpenPanel openPanel]; |
1370 | 1351 |
|
1371 | | - //allow files |
| 1352 | + //configure panel |
1372 | 1353 | panel.canChooseFiles = YES; |
1373 | | - |
1374 | | - //disallow directories |
1375 | 1354 | panel.canChooseDirectories = NO; |
1376 | | - |
1377 | | - //disable multiple selections |
1378 | 1355 | panel.allowsMultipleSelection = NO; |
1379 | | - |
1380 | | - //can open app bundles |
| 1356 | + panel.allowedFileTypes = @[@"json"]; |
1381 | 1357 | panel.treatsFilePackagesAsDirectories = YES; |
1382 | 1358 |
|
1383 | 1359 | //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]]; |
1412 | 1361 |
|
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) { |
1427 | 1364 |
|
1428 | | - NSMutableSet *addedPaths = nil; |
1429 | | - NSMutableSet *removedPaths = nil; |
| 1365 | + //load previous scan |
| 1366 | + prevScan = [NSString stringWithContentsOfURL:panel.URL encoding:NSUTF8StringEncoding error:nil]; |
1430 | 1367 |
|
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]; |
1449 | 1370 |
|
1450 | | - prevItems = prevScanContents[key]; |
| 1371 | + //parse current scan JSON |
| 1372 | + currentScanContents = [NSJSONSerialization JSONObjectWithData:[[self scanToJSON] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; |
1451 | 1373 |
|
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]; |
1495 | 1382 |
|
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]]; |
1524 | 1383 | } |
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]; |
1538 | 1392 | } |
1539 | 1393 | } |
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 | | - } |
1568 | 1394 |
|
1569 | 1395 | return; |
1570 | 1396 | } |
|
0 commit comments