Net-Dropbox

 view release on metacpan or  search on metacpan

lib/Net/Dropbox.pm  view on Meta::CPAN

package Net::Dropbox;
our $VERSION = '1.091510';

# ABSTRACT: Communicate with local Dropbox daemon

use warnings;
use strict;

use File::HomeDir;

use Moose;
use Moose::Util::TypeConstraints;

use MooseX::StrictConstructor;

use IO::Socket;
use IO::Socket::UNIX;

use Encode;

subtype SocketExists 
	=> as 'Str'
	=> where { -S $_ }
	=> message { "The socket name you provided, $_, is not a valid socket" };

has 'command_socket' => ( 
	is      => 'ro', 
	isa     => 'SocketExists', 
	default => File::HomeDir->my_home.'/.dropbox/command_socket' 
);

has 'hold_connection_open' => ( 
	is      => 'ro', 
	isa     => 'Bool', 
	default => 0 
);

# This is hardcoded into the official C client so I'm mirroring it
has 'max_args' => (
	is         => 'ro',
	isa        => 'Int',
	default    => 20,
	init_arg   => undef
);

has '_socket' => (
	is        => 'rw',
	isa       => 'FileHandle',
	init_arg  => undef
);



sub file_status { #{{{
	my $self = shift;
	my $file = shift;

	my $res = $self->_send_and_fetch(
		"icon_overlay_file_status",
		{ path => $file },
	);

	return $res;
} #}}}





sub dir_status { #{{{
	my $self = shift;
	my $dir  = shift;

	return { ok => 0, status => 'not a dir' } 
		unless( -d $dir );

	my $res = $self->_send_and_fetch(
		"icon_overlay_file_status",
		{ path => $dir },
	);

	if($res->{ok} == 1) {
		my $res2 = $self->_send_and_fetch(
			"get_folder_tag",
			{ path => $dir },
		);

		if($res2->{ok} == 1) {
			for my $key ( keys %$res2 ) {
				$res->{$key} = $res2->{$key};
			}
			
		} else {
			return $res2;
		}
	}

	return $res;

} #}}}


######################
# Internal methods
######################


sub _send_and_fetch { #{{{
	my $self = shift;
	my $cmd = shift;
	my $args = shift;

	my $cmdtxt = $self->_build_command($cmd, $args);

	$self->_open_connection();
	my $sock = $self->_socket();
	print $sock $cmdtxt or die "print : $!\n";

	my $buf = $self->_read_sock_buffer();
	$self->_close_connection();
	
	my $res = $self->_parse_response($buf);

	return $res;
} #}}}

sub _build_command { #{{{
	my $self    = shift;
	my $command = shift;
	my $args    = shift;

	my $output = "$command\n";
	foreach my $arg (sort keys %$args) {
		$output .= "$arg\t$args->{$arg}\n";
	}
	$output .= "done\n";

	$output = encode("utf8", $output);



( run in 3.333 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )