Created
April 21, 2010 19:12
-
-
Save datenimperator/374268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# MyDocument.rb | |
# xing2 | |
# | |
# Created by Christian on 17.04.10. | |
# Copyright (c) 2010 __MyCompanyName__. All rights reserved. | |
# | |
require 'osx/cocoa' | |
include OSX | |
class ImageDownloadOperation < OSX::NSOperation | |
attr_accessor :card, :document | |
def main | |
@card.photo.each { |photo| | |
if photo.opts[:value]=="URL" | |
p "#{@card.n}: #{photo.value}" | |
req = OSX::NSURLRequest.requestWithURL_cachePolicy_timeoutInterval( | |
OSX::NSURL.URLWithString(photo.value), | |
OSX::NSURLRequestReloadIgnoringCacheData, # don't cache | |
15 # 5 seconds timeout | |
) | |
resp = OSX::NSURLResponse.alloc.init | |
error = OSX::NSError.alloc.init | |
data = OSX::NSURLConnection.sendSynchronousRequest_returningResponse_error(req, resp, error) | |
if data.nil? | |
OSX::NSLog("Error #{error}") | |
return | |
end | |
if data.length<1 | |
OSX::NSLog("Empty response: #{data.length}") | |
return | |
end | |
begin | |
imagedata = data.bytes.bytestr(data.length) | |
photo.opts = {:type =>'JPEG', :encoding => 'BASE64'} | |
b64 = imagedata.to_a.pack('m') | |
photo.value = b64.tr("\n", '') | |
@document.send :download_done | |
rescue RuntimeError => err | |
OSX::NSLog("Error #{err}") | |
end | |
end | |
} | |
end | |
end | |
class VCardDocument < OSX::NSDocument | |
ib_outlets :convert, :crop, :clean, :open | |
ib_outlets :progress, :status | |
def initialize | |
@convert = @crop = @clean = @open = nil | |
@cards = [] | |
@edited = false | |
@queue = NSOperationQueue.alloc.init | |
@queue.setMaxConcurrentOperationCount(1) | |
end | |
def edited | |
@edited | |
end | |
def isDocumentEdited | |
@edited | |
end | |
def edited=(value) | |
@edited = value | |
window.setDocumentEdited(value) | |
end | |
def awakeFromNib | |
update_status | |
end | |
def windowNibName | |
# Implement this to return a nib to load OR implement | |
# -makeWindowControllers to manually create your controllers. | |
return "VCardDocument" | |
end | |
def btnConvert(sender) | |
external_cards = @cards.select {|c| c.external_image? } | |
@progress.setMaxValue(external_cards.length.to_f) | |
@progress.setDoubleValue(0.0) | |
external_cards.each{|c| download(c)} | |
end | |
ib_action :btnConvert | |
def download_done | |
@progress.setDoubleValue(@progress.doubleValue + 1.0) | |
# edited = true | |
# updateChangeCount(NSChangeDone) | |
p @queue.operations.any? {|o| o.isFinished == true} | |
end | |
def download(card) | |
if card.external_image? | |
op = ImageDownloadOperation.new | |
op.card = card | |
op.document = self | |
@queue.addOperation(op) | |
end | |
end | |
def selected_options | |
{ | |
:convert => (@convert.state == 1), | |
:crop => (@crop.state == 1), | |
:clean => (@clean.state == 1), | |
:open => (@open.state == 1) | |
} | |
end | |
def update_status | |
return if (@status.nil? || @cards.nil?) | |
external_cards = @cards.select {|c| c.external_image? } | |
@status.setStringValue("#{external_cards.length}/#{@cards.length}") | |
end | |
def readFromURL_ofType_error(url, type, error) | |
if type == 'vCard' | |
file = File.new(File.expand_path(url.path), "r") | |
while (line = file.gets) | |
case line.strip | |
when /^BEGIN:VCARD/ | |
data = [] | |
when /^END:VCARD/ | |
@cards << VCard.new(data) | |
data = nil | |
else | |
data << line unless data.nil? | |
end | |
end | |
file.close | |
update_status | |
edited = false | |
return true | |
end | |
false | |
end | |
def writeToURL_ofType_error(url, type, error) | |
if type=='vCard' | |
File.open(File.expand_path(url.path), "w") do |file| | |
@cards.each_with_index do |card, i| | |
begin | |
file.puts card.to_2_1 | |
rescue Object=>err | |
OSX::NSLog("Error while converting: #{err}") | |
end | |
end | |
file.close | |
end | |
edited = false | |
# if options[:open] | |
# system("/usr/bin/open -b com.apple.AddressBook #{url.path}") | |
# end | |
return true | |
end | |
false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment