Skip to content

Commit

Permalink
generate QRCode for initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
fuji246 committed Jul 20, 2019
1 parent c06f176 commit 494bb3b
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 34 deletions.
6 changes: 6 additions & 0 deletions LomoAgent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3A2FE40022E2B339000022B0 /* QRImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A2FE3FF22E2B339000022B0 /* QRImage.swift */; };
3A35C5D92240B5F30030FF1B /* avconv in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3A35C5D62240B5EA0030FF1B /* avconv */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
3A35C5DA2240B5F30030FF1B /* lomod in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3A35C5D52240B5E90030FF1B /* lomod */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
3A35C5DC2240B6040030FF1B /* avconv in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3A35C5CF2240B5A90030FF1B /* avconv */; };
Expand Down Expand Up @@ -48,6 +49,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
3A2FE3FF22E2B339000022B0 /* QRImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRImage.swift; sourceTree = "<group>"; };
3A2FE40122E2BC34000022B0 /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = "<group>"; };
3A35C5CE2240B5A90030FF1B /* lomod */ = {isa = PBXFileReference; lastKnownFileType = folder; name = lomod; path = dependencies/lomod/Contents/Frameworks/lomod; sourceTree = "<group>"; };
3A35C5CF2240B5A90030FF1B /* avconv */ = {isa = PBXFileReference; lastKnownFileType = folder; name = avconv; path = dependencies/lomod/Contents/Frameworks/avconv; sourceTree = "<group>"; };
3A35C5D52240B5E90030FF1B /* lomod */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = lomod; path = dependencies/lomod/Contents/MacOS/lomod; sourceTree = "<group>"; };
Expand Down Expand Up @@ -131,11 +134,13 @@
isa = PBXGroup;
children = (
3A3896FB222F77FA004A2001 /* AppDelegate.swift */,
3A2FE3FF22E2B339000022B0 /* QRImage.swift */,
3A389709222F8168004A2001 /* StatusMenuController.swift */,
3A3896FD222F7801004A2001 /* Assets.xcassets */,
3A3896FF222F7801004A2001 /* MainMenu.xib */,
3A38970D22321A03004A2001 /* PreferencesWindow.swift */,
3A35C5E322435B710030FF1B /* PreferencesWindow.xib */,
3A2FE40122E2BC34000022B0 /* Bridging-Header.h */,
3A35C5E8224365340030FF1B /* AboutWindow.swift */,
3AC3E845229F9B60004EA1D7 /* HyperlinkTextField.swift */,
3A35C5EE22436CBA0030FF1B /* AboutWindow.xib */,
Expand Down Expand Up @@ -241,6 +246,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3A2FE40022E2B339000022B0 /* QRImage.swift in Sources */,
3A38970F22321A03004A2001 /* PreferencesWindow.swift in Sources */,
3A38970A222F8168004A2001 /* StatusMenuController.swift in Sources */,
3A3896FC222F77FA004A2001 /* AppDelegate.swift in Sources */,
Expand Down
50 changes: 28 additions & 22 deletions LomoAgent/Base.lproj/PreferencesWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<connections>
<outlet property="debugModeCheckBox" destination="6In-C0-Ox8" id="Elp-qb-8ve"/>
<outlet property="homeDirTextField" destination="eQt-yd-8Oh" id="iWN-ft-Og1"/>
<outlet property="imageQRCode" destination="Ha9-zw-3Td" id="CaF-1f-0Zm"/>
<outlet property="openButton" destination="AyW-Hh-bcP" id="Dvx-Bf-pkk"/>
<outlet property="portTextField" destination="8OA-ER-QGA" id="gze-4u-eHl"/>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
Expand All @@ -20,14 +21,14 @@
<window title="Settings" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="F0z-JX-Cv5">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="300" height="200"/>
<rect key="contentRect" x="196" y="240" width="360" height="200"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<view key="contentView" wantsLayer="YES" id="se5-gp-TjO">
<rect key="frame" x="0.0" y="0.0" width="300" height="200"/>
<rect key="frame" x="0.0" y="0.0" width="360" height="200"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dPJ-v8-FX0">
<rect key="frame" x="38" y="150" width="100" height="17"/>
<rect key="frame" x="38" y="163" width="100" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Home Directory" id="lAc-fy-Miv">
<font key="font" usesAppearanceFont="YES"/>
Expand All @@ -36,7 +37,7 @@
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3zh-D9-PhM">
<rect key="frame" x="40" y="120" width="217" height="22"/>
<rect key="frame" x="40" y="128" width="280" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="eQt-yd-8Oh">
<font key="font" metaFont="system"/>
Expand All @@ -45,7 +46,7 @@
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AyW-Hh-bcP">
<rect key="frame" x="62" y="74" width="73" height="32"/>
<rect key="frame" x="174" y="153" width="73" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Open" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="CTw-zS-X9b">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -56,30 +57,18 @@
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="FeW-Jp-XEh">
<rect key="frame" x="32" y="25" width="30" height="17"/>
<rect key="frame" x="38" y="85" width="30" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Port" id="VTa-Lr-Xxo">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="8OA-ER-QGA">
<rect key="frame" x="68" y="23" width="59" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="X2E-Ty-WCS">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="onPortChange:" target="-2" id="s9f-52-8ak"/>
</connections>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6In-C0-Ox8">
<rect key="frame" x="159" y="25" width="100" height="18"/>
<rect key="frame" x="38" y="35" width="122" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Debug Mode" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="439-ns-a3v">
<buttonCell key="cell" type="check" title="Debug Mode" bezelStyle="regularSquare" imagePosition="left" alignment="center" state="on" inset="2" id="439-ns-a3v">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -88,7 +77,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9j3-TC-mnI">
<rect key="frame" x="155" y="74" width="79" height="32"/>
<rect key="frame" x="247" y="153" width="79" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Select" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="o4F-iv-Nxx">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -98,12 +87,29 @@
<action selector="onSelectPath:" target="-2" id="Hqr-jW-W4z"/>
</connections>
</button>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ha9-zw-3Td">
<rect key="frame" x="220" y="14" width="100" height="100"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="QnP-UT-T1E"/>
</imageView>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="8OA-ER-QGA">
<rect key="frame" x="87" y="82" width="59" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="X2E-Ty-WCS">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="onPortChange:" target="-2" id="s9f-52-8ak"/>
</connections>
</textField>
</subviews>
</view>
<connections>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="165" y="141"/>
<point key="canvasLocation" x="190" y="141"/>
</window>
</objects>
</document>
14 changes: 14 additions & 0 deletions LomoAgent/Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Bridging-Header.h
// LomoAgent
//
// Created by Jiantao Fu on 7/19/19.
// Copyright © 2019 lomoware. All rights reserved.
//

#ifndef Bridging_Header_h
#define Bridging_Header_h

#include <ifaddrs.h>

#endif /* Bridging_Header_h */
4 changes: 2 additions & 2 deletions LomoAgent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>ff6d861</string>
<string>c06f176</string>
<key>CFBundleVersion</key>
<string>2019_06_04.09_09_24.0.ff6d861</string>
<string>2019_07_19.21_11_50.0.c06f176</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
77 changes: 70 additions & 7 deletions LomoAgent/PreferencesWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,43 @@ let PREF_HOME_DIR = "PrefHomeDir"
let PREF_PORT = "PrefPort"

extension Notification.Name {
static let NotifyHomeDirChanged = NSNotification.Name("NotifyHomeDirChanged")
static let NotifySettingsChanged = NSNotification.Name("NotifySettingsChanged")
static let NotifyExit = NSNotification.Name("NotifyExit")
static let NotifyStart = NSNotification.Name("NotifyStart")
}

func getIFAddresses() -> [String] {
var addresses = [String]()

// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return [] }
guard let firstAddr = ifaddr else { return [] }

// For each interface ...
for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let flags = Int32(ptr.pointee.ifa_flags)
let addr = ptr.pointee.ifa_addr.pointee

// Check for running IPv4. Skip the loopback interface and IPv6 interfaces(AF_INET6).
if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
if addr.sa_family == UInt8(AF_INET) {

// Convert interface address to a human readable string:
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST) == 0) {
let address = String(cString: hostname)
addresses.append(address)
}
}
}
}

freeifaddrs(ifaddr)
return addresses
}

class PreferencesWindow: NSWindowController, NSWindowDelegate {

@IBOutlet weak var homeDirTextField: NSTextFieldCell!
Expand All @@ -29,14 +61,26 @@ class PreferencesWindow: NSWindowController, NSWindowDelegate {

@IBOutlet weak var openButton: NSButton!

@IBOutlet weak var imageQRCode: NSImageView!

@IBAction func onDebugModeClick(_ sender: Any) {
UserDefaults.standard.set(debugModeCheckBox.state == .on, forKey: PREF_DEBUG_MODE)
let oldState = UserDefaults.standard.bool(forKey: PREF_DEBUG_MODE)
let newState = (debugModeCheckBox.state == .on)
if newState != oldState {
UserDefaults.standard.set(newState, forKey: PREF_DEBUG_MODE)
NotificationCenter.default.post(name: .NotifySettingsChanged, object: self)
}
}

@IBAction func onPortChange(_ sender: Any) {
let oldPort = UserDefaults.standard.string(forKey: PREF_PORT)
let port = portTextField.stringValue
os_log("Save port: %{public}s", log: .ui, port)
UserDefaults.standard.set(port, forKey: PREF_PORT)
if oldPort != port {
os_log("Save port: %{public}s", log: .ui, port)
UserDefaults.standard.set(port, forKey: PREF_PORT)
generateQRCode()
NotificationCenter.default.post(name: .NotifySettingsChanged, object: self)
}
}

@IBAction func onOpenPath(_ sender: Any) {
Expand All @@ -58,10 +102,14 @@ class PreferencesWindow: NSWindowController, NSWindowDelegate {
if let result = dialog.url {
homeDirTextField.stringValue = result.path
openButton.isEnabled = true
UserDefaults.standard.set(result.path, forKey: PREF_HOME_DIR)
os_log("Save home dir: %{public}s", log: .ui, homeDirTextField.stringValue)

NotificationCenter.default.post(name: .NotifyHomeDirChanged, object: self)
let oldHomeDir = UserDefaults.standard.string(forKey: PREF_HOME_DIR)
if oldHomeDir != homeDirTextField.stringValue {
UserDefaults.standard.set(homeDirTextField.stringValue, forKey: PREF_HOME_DIR)
os_log("Save home dir: %{public}s", log: .ui, homeDirTextField.stringValue)
NotificationCenter.default.post(name: .NotifySettingsChanged, object: self)
// todo: move files in directory
}
}
}
}
Expand All @@ -76,6 +124,7 @@ class PreferencesWindow: NSWindowController, NSWindowDelegate {
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
self.window?.level = .floating

if UserDefaults.standard.bool(forKey: PREF_DEBUG_MODE) {
debugModeCheckBox.state = .on
Expand All @@ -100,6 +149,20 @@ class PreferencesWindow: NSWindowController, NSWindowDelegate {
portTextField.stringValue = "8000"
UserDefaults.standard.set(portTextField.stringValue, forKey: PREF_PORT)
}

generateQRCode()
}

func generateQRCode() {
imageQRCode.wantsLayer = true
let addresses = getIFAddresses()
if let firstAddr = addresses.first {
let url = "http://\(firstAddr):\(portTextField.stringValue)"
guard let data = url.data(using: String.Encoding.utf8, allowLossyConversion: false) else {
return
}
imageQRCode.image = QRCodeImageWith(data: data, size: imageQRCode.frame.size.width)
}
}

func windowWillClose(_ notification: Notification) {
Expand Down
Loading

0 comments on commit 494bb3b

Please sign in to comment.