写真集

Camera:Panasonic LUMIX DMC-LX3
Vine Linux と Ubuntu の設定方法など

Objective-C Safari の Cookies.binarycookies からCookie を取得する

2012-07-24 19:40:15 | MacOS

Mac OS X Safari Cookie を取得する。

クッキーがバイナリファイルになってしまったので、コマンドラインで取得できるようにしました。
Cookies.binarycookies から name,value,domain,pathを標準出力します。
awkなどを使って値が取得できます。
download safari-cookies

SafariCookies

SafariCookies.m

----------------------------------------------------------
// cc -framework Foundation -o safari-cookies SafariCookies.m
#import <Foundation/Foundation.h>

int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
	NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *c in [cookieStorage cookies])
    {
        NSString *cookie = [NSString stringWithFormat:@"%s %s %s %s", c.name.UTF8String, c.value.UTF8String, c.domain.UTF8String, c.path.UTF8String];
        puts(cookie.UTF8String);
    }

	[pool release];
    return 0;
}
----------------------------------------------------------
$ cc -framework Foundation -o safari-cookies SafariCookies.m

SafariCookies llvm ARC コンパイル版

SafariCookiesArc.m

----------------------------------------------------------
//clang -fobjc-arc -framework Foundation -o safari-cookies  SafariCookiesArc.m
#import <Foundation/Foundation.h>


int main(int argc, char *argv[])
{
	@autoreleasepool {
    
	NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
	for (NSHTTPCookie *c in [cookieStorage cookies])
	{
		NSString *cookie = [NSString stringWithFormat:@"%s %s %s %s", c.name.UTF8String, c.value.UTF8String, c.domain.UTF8String, c.path.UTF8String];
		puts(cookie.UTF8String);
	 }

	}
    return 0;
}
----------------------------------------------------------
$ clang -fobjc-arc -framework Foundation -o safari-cookies SafariCookiesArc.m

awk での取得サンプル

safari-cookies.sh
----------------------------------------------------------
#!/bin/sh
CookieName="SSID"
CookieValue=".+"
Domain=".google.co.jp"
Path="\/"
safari-cookies | awk "/$CookieName $CookieValue $Domain $Path/" | awk '{ print $2 }'
----------------------------------------------------------
$ sh safari-cookies.sh

Ubuntu で Objective-C のコンパイル

$ sudo apt-get install gnustep-devel gnustep-make

Makefile
----------------------------------------------------------
CC = gcc
CFLAGS = $(shell gnustep-config --objc-flags) 
INCLUDES = 
LIBS = -lgnustep-base
OBJS = SafariCookies.o
TARGET = safari-cookies

$(TARGET): $(OBJS)
	$(CC) -o $@ $(OBJS) $(LIBS)

%.o: %.m
	$(CC) $(CFLAGS) -c $*.m -o $*.o

clean:
	rm -f $(TARGET) $(OBJS)
----------------------------------------------------------
$ make


Android ListView を layout の XML を使用してカスタマイズ

2012-07-12 21:52:02 | Android

layout に comment_list_row.xml を作成します。


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
    <TextView
        android:id="@+id/commentNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#55EEFF"
        android:textSize="12dp"
        android:textStyle="bold"  />

    <TextView
        android:id="@+id/commentUserID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="12345678"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="12dp"
        android:textStyle="bold" />

    </LinearLayout>

    <TextView
        android:id="@+id/commentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="こんばんは"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15dp" />

</LinearLayout>

カスタム ListViewを作成します。BaseAdapterを拡張しました。
LayoutInflater を使用すると、 R.layout.comment_list_row が読み込めます。

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NicoCommentListView {
	private ListView listView;
	private CommentAdapter adapter;
	
	public NicoCommentListView(ListView listView, Context context){
		this.listView = listView;
		adapter = new CommentAdapter(context, R.layout.comment_list_row);
		setNicoCommentListView();
	}
	private void setNicoCommentListView(){
		listView.setAdapter(adapter);
		listView.setFastScrollEnabled(true);
	}
	public void append(String[] comment){
		adapter.add(new CommentData(comment[0], comment[1], comment[2]));
		adapter.notifyDataSetChanged();
		listView.smoothScrollToPosition(listView.getCount());
	}
	
	private class CommentData {
		public String No;
		public String UserID;
		public String Comment;
		public CommentData(String no, String userID, String comment){
			No = no;
			UserID = userID;
			Comment = comment;
		}
	}
	
	private class CommentAdapter extends BaseAdapter {
		private final int oddColor = Color.rgb(0, 0, 0);
		private final int evenColor = Color.rgb(50, 50, 50);
		
		private Context context;
		private int textViewResourceId;
		private List<CommentData> list = new ArrayList<CommentData>();
		
		public CommentAdapter(Context context, int textViewResourceId) {
			this.context = context;
			this.textViewResourceId = textViewResourceId;
		}
		public void add(CommentData data){
			list.add(data);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(textViewResourceId, null);
			TextView tvNo = (TextView)view.findViewById(R.id.commentNo);
			TextView tvUserID= (TextView)view.findViewById(R.id.commentUserID);
			TextView tvComment= (TextView)view.findViewById(R.id.commentText);
			
			tvNo.setText(list.get(position).No);
			tvUserID.setText(list.get(position).UserID);
			tvComment.setText(list.get(position).Comment);
			
			if (position%2 == 0) {
				view.setBackgroundColor(evenColor);
			}
			else {
				view.setBackgroundColor(oddColor);
			}
			
			return view;
		}
		@Override
		public int getCount() {
			return list.size();
		}
		@Override
		public Object getItem(int position) {
			return list.get(position);
		}
		@Override
		public long getItemId(int position) {
			return list.indexOf(list.get(position));
		}
	}
}