77
88import Foundation
99import Cocoa
10+ import CoreData
1011
1112@main
1213class SubnetCalcAppDelegate : NSObject , NSApplicationDelegate , NSWindowDelegate , NSTableViewDataSource {
@@ -95,6 +96,8 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
9596 private var ipsc : IPSubnetCalc ?
9697 private var subnetsVLSM = [ ( Int, String, String) ] ( )
9798 private var globalMaskVLSM : UInt32 !
99+ private var container : NSPersistentContainer !
100+ private var history = [ AddrHistory] ( )
98101
99102 //**********************
100103 //Private IPv4 functions
@@ -1427,7 +1430,6 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
14271430 /**
14281431 Triggered when the user hit Enter key in the IP address field
14291432
1430-
14311433 - Parameter sender: sender of the action
14321434
14331435 */
@@ -1437,15 +1439,23 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
14371439 if ( ( sender as? NSTextField ) ? . stringValue) != nil {
14381440 if ( sender. stringValue != " " ) {
14391441 if ( addrField. indexOfItem ( withObjectValue: sender. stringValue!) == NSNotFound) {
1440- if ( addrField. numberOfItems = = Constants . maxAddrHistory) {
1442+ if ( addrField. numberOfItems > = Constants . maxAddrHistory) {
14411443 addrField. removeItem ( at: 0 )
1444+ if ( history. count > 0 ) {
1445+ container. viewContext. delete ( history [ 0 ] )
1446+ history. remove ( at: 0 )
1447+ saveHistory ( )
1448+ }
14421449 }
14431450 addrField. addItem ( withObjectValue: sender. stringValue!)
1451+ let historyItem = AddrHistory ( context: container. viewContext)
1452+ historyItem. address = sender. stringValue!
1453+ history. append ( historyItem)
1454+ saveHistory ( )
14441455 }
14451456 self . calc ( sender)
14461457 }
14471458 }
1448-
14491459 }
14501460
14511461 /**
@@ -1646,6 +1656,61 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
16461656 }
16471657 }
16481658
1659+ /**
1660+ Clear address IP field history
1661+
1662+ Triggered when the user select Clear history in the window menu.
1663+
1664+ - Parameter sender: non used
1665+
1666+ */
1667+ @IBAction func clearHistory( _ sender: AnyObject )
1668+ {
1669+ for _ in ( 0 ... addrField. numberOfItems- 1 ) {
1670+ addrField. removeItem ( at: 0 )
1671+ }
1672+ for _ in ( 0 ... history. count- 1 ) {
1673+ container. viewContext. delete ( history [ 0 ] )
1674+ history. remove ( at: 0 )
1675+ saveHistory ( )
1676+ }
1677+ }
1678+
1679+ /**
1680+ Save the address IP field history for future App sessions
1681+ */
1682+ private func saveHistory( ) {
1683+ if container. viewContext. hasChanges {
1684+ do {
1685+ try container. viewContext. save ( )
1686+ } catch {
1687+ print ( " An error occurred while saving: \( error) " )
1688+ }
1689+ }
1690+ }
1691+
1692+ /**
1693+ Load in the address IP field the history of previous App sessions
1694+ */
1695+ private func loadHistory( ) {
1696+ container = NSPersistentContainer ( name: " SubnetCalc " )
1697+ container. loadPersistentStores { storeDescription, error in
1698+ if let error = error {
1699+ print ( " Unresolved error \( error) " )
1700+ }
1701+ }
1702+ do {
1703+ history = try container. viewContext. fetch ( AddrHistory . fetchRequest ( ) )
1704+ //print("Got \(history.count) items")
1705+ for item in history {
1706+ //print(item.address)
1707+ addrField. addItem ( withObjectValue: item. address)
1708+ }
1709+ } catch {
1710+ print ( " Fetch history failed " )
1711+ }
1712+ }
1713+
16491714 /**
16501715 Auto invoked when the Main Windows has been resized
16511716 */
@@ -1667,6 +1732,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
16671732 */
16681733 func applicationDidFinishLaunching( _ aNotification: Notification ) {
16691734 // Insert code here to initialize your application
1735+ loadHistory ( )
16701736 initSubnetsTab ( )
16711737 initCIDRTab ( )
16721738 initIPv6Tab ( )
0 commit comments