Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Binded to visibility API.

  • Loading branch information...
commit 218e8a9d154d0e9c2fca3d3e2ca6d8faae550f40 1 parent e8d14f2
@arcusfelis authored
View
21 q_src/class/cascadae/Container.js
@@ -5,7 +5,12 @@
qx.Class.define("cascadae.Container",
{
extend : qx.ui.container.Composite,
-
+ events :
+ {
+ "activated" : "qx.event.type.Event",
+ "deactivated" : "qx.event.type.Event",
+ "r_checkVisibility" : "qx.event.type.Event"
+ },
@@ -63,6 +68,12 @@ qx.Class.define("cascadae.Container",
// Set "dead" zones (never will get focus)
this.__header.setKeepFocus(true);
this.__toolBar.setKeepFocus(true);
+
+ this.__pageVisibility = qx.bom.PageVisibility.getInstance();
+ this.__pageVisibility.addListener("change",
+ this.__checkPageVisibility,
+ this);
+ socket.registerObject(this);
},
members :
@@ -77,6 +88,14 @@ qx.Class.define("cascadae.Container",
__viewLoaded : false,
__activeView : "",
+ __checkPageVisibility: function()
+ {
+ if (this.__pageVisibility.isHidden())
+ this.fireEvent("deactivated");
+ else
+ this.fireEvent("activated");
+ },
+
getRoot : function()
{
return this.__application.getRoot();
View
6 src/cascadae_files.erl
@@ -46,7 +46,7 @@ deactivate(Srv) ->
%% ------------------------------------------------------------------
init([Session, Tag]) ->
- TRef = timer:send_interval(5000, update_tree),
+ {ok, TRef} = timer:send_interval(5000, update_tree),
State = #files_state{
session_pid=Session,
session_tag=Tag,
@@ -59,6 +59,7 @@ handle_call(_Request, _From, State) ->
handle_cast(activate, State=#files_state{update_tree_tref=undefined}) ->
+ lager:info("Activate timer.", []),
{ok, TRef} = timer:send_interval(5000, update_tree),
{noreply, State#files_state{update_tree_tref=TRef}};
handle_cast(activate, State=#files_state{}) ->
@@ -66,7 +67,8 @@ handle_cast(activate, State=#files_state{}) ->
handle_cast(deactivate, State=#files_state{update_tree_tref=undefined}) ->
{noreply, State};
handle_cast(deactivate, State=#files_state{update_tree_tref=TRef}) ->
- ok = timer:cancel(TRef),
+ lager:info("Deactivate timer.", []),
+ {ok, cancel} = timer:cancel(TRef),
{noreply, State#files_state{update_tree_tref=undefined}};
%% Same torrent.
handle_cast({request, TorrentID, ParentFileIDs},
View
102 src/cascadae_peers.erl
@@ -1,6 +1,108 @@
-module(cascadae_peers).
-export([all_peers/0]).
+-behaviour(gen_server).
+-define(SERVER, ?MODULE).
+-record(peers_state, {
+ session_pid,
+ session_tag,
+ torrent_ids,
+ update_table_tref
+}).
+
+%% ------------------------------------------------------------------
+%% API Function Exports
+%% ------------------------------------------------------------------
+
+-export([start_link/2,
+ set_torrent_list/2,
+ activate/1,
+ deactivate/1]).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Exports
+%% ------------------------------------------------------------------
+
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+start_link(Session, Tag) ->
+ gen_server:start_link(?MODULE, [Session, Tag], []).
+
+set_torrent_list(Srv, TorrentIDs) ->
+ gen_server:cast(Srv, {set_torrent_list, TorrentIDs}).
+
+activate(Srv) ->
+ gen_server:cast(Srv, activate).
+
+deactivate(Srv) ->
+ gen_server:cast(Srv, deactivate).
+
+%% ------------------------------------------------------------------
+%% gen_server Function Definitions
+%% ------------------------------------------------------------------
+
+init([Session, Tag]) ->
+ TRef = timer:send_interval(5000, update_table),
+ State = #peers_state{
+ session_pid=Session,
+ session_tag=Tag,
+ update_table_tref=TRef
+ },
+ {ok, State}.
+
+handle_call(_Request, _From, State) ->
+ {reply, ok, State}.
+
+
+handle_cast(activate, State=#peers_state{update_table_tref=undefined}) ->
+ {ok, TRef} = timer:send_interval(5000, update_table),
+ {noreply, State#peers_state{update_table_tref=TRef}};
+handle_cast(activate, State=#peers_state{}) ->
+ {noreply, State};
+handle_cast(deactivate, State=#peers_state{update_table_tref=undefined}) ->
+ {noreply, State};
+handle_cast(deactivate, State=#peers_state{update_table_tref=TRef}) ->
+ ok = timer:cancel(TRef),
+ {noreply, State#peers_state{update_table_tref=undefined}}.
+
+
+handle_info(update_table, State) ->
+ lager:debug("Handle update_table timeout.", []),
+ #peers_state{
+ torrent_ids=TorrentIDs,
+ session_pid=Session,
+ session_tag=Tag
+ } = State,
+% cascadae_session:send(Session, {Tag, {diff_list, TorrentID, Diff}})
+ {noreply, State#peers_state{}}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%% ------------------------------------------------------------------
+%% Internal Function Definitions
+%% ------------------------------------------------------------------
+
+
+
+
+
+
+
+
+
+%% etorrent_peer_states:get_send_rate(Id, Pid)
+%% etorrent_peer_states:get_recv_rate(Id, Pid)
+
+
all_peers() ->
%{pid, Pid}, {ip, IP}, {port, Port},
%{torrent_id, TorrentId},
View
46 src/cascadae_session.erl
@@ -10,7 +10,11 @@
%% Id of the ETS table with `#object{}' records.
obj_tbl,
%% Pid of `cascadae_files' worker.
- files_pid
+ files_pid,
+ %% Is an interval timer for updates active?
+ is_files_active,
+ is_files_visible,
+ is_page_visible
}).
-define(OBJ_TBL, cascadae_object_register).
-define(HUB, cascadae_hub).
@@ -48,8 +52,9 @@ recv(_, _, {event, <<>>, <<"deactivated">>, Meta}=Mess, State) ->
#object{path=Path} = extract_object(Hash, State),
case Path of
[<<"cascadae">>,<<"files">>,<<"Tree">>] when is_pid(FilesPid) ->
- cascadae_files:deactivate(FilesPid),
- {ok, State};
+ {ok, check_file_visibility(State#sess_state{is_files_visible=false})};
+ [<<"cascadae">>,<<"Container">>] ->
+ {ok, check_file_visibility(State#sess_state{is_page_visible=false})};
_ ->
lager:debug("Ignore ~p.", [Mess]),
{ok, State}
@@ -62,8 +67,9 @@ recv(_, _, {event, <<>>, <<"activated">>, Meta}=Mess, State) ->
#object{path=Path} = extract_object(Hash, State),
case Path of
[<<"cascadae">>,<<"files">>,<<"Tree">>] when is_pid(FilesPid) ->
- cascadae_files:activate(FilesPid),
- {ok, State};
+ {ok, check_file_visibility(State#sess_state{is_files_visible=true})};
+ [<<"cascadae">>,<<"Container">>] ->
+ {ok, check_file_visibility(State#sess_state{is_page_visible=true})};
_ ->
lager:debug("Ignore ~p.", [Mess]),
{ok, State}
@@ -104,7 +110,8 @@ recv(Session, _, {event, <<>>, <<"d_childrenRequest">>, Meta}, State) ->
Pid -> Pid
end,
cascadae_files:request(FilesPid, TorrentID, FileIDs),
- {ok, State#sess_state{files_pid=FilesPid}};
+ {ok, State#sess_state{files_pid=FilesPid,
+ is_files_active=true, is_files_visible=true}};
recv(Session, _, {event, <<>>, <<"d_wishFiles">>, Meta}, State) ->
spawn_link(fun() ->
Data = proplists:get_value(<<"data">>, Meta),
@@ -231,13 +238,22 @@ register_object(Path=[<<"cascadae">>,<<"files">>,<<"Tree">>], Hash, State) ->
add_event_listener(Session, Hash, <<"activated">>),
add_event_listener(Session, Hash, <<"deactivated">>),
store_object(Path, Hash, State);
+register_object(Path=[<<"cascadae">>,<<"Container">>], Hash, State) ->
+ Session = self(),
+ add_event_listener(Session, Hash, <<"activated">>),
+ add_event_listener(Session, Hash, <<"deactivated">>),
+ add_event_listener(Session, Hash, <<"r_checkVisibility">>),
+ store_object(Path, Hash, State);
+
register_object(Path=[<<"cascadae">>,<<"peers">>,<<"Table">>], Hash, State) ->
-% Session = self(),
+ Session = self(),
% proc_lib:spawn_link(fun() ->
% Peers = cascadae_peers:all_peers(),
% fire_data_event(Session, Hash, <<"rd_dataLoadCompleted">>,
% [{<<"rows">>, Peers}])
% end),
+ add_event_listener(Session, Hash, <<"activated">>),
+ add_event_listener(Session, Hash, <<"deactivated">>),
store_object(Path, Hash, State);
register_object(Path, Hash, State) ->
lager:warning("Cannot register objest ~p:~p.", [Path, Hash]),
@@ -314,3 +330,19 @@ decode_wishes(Wishes) ->
if is_binary(V) -> binary_to_existing_atom(V, utf8);
true -> V
end} || {K, V} <- X, K =/= <<"name">>] || X <- Wishes].
+
+
+
+%% The timer must be active only when the page is visible and file view is active.
+check_file_visibility(State=#sess_state{is_page_visible=true,
+ is_files_visible=true,
+ is_files_active=false,
+ files_pid=FilesPid}) ->
+ cascadae_files:activate(FilesPid),
+ State#sess_state{is_files_active=true};
+check_file_visibility(State=#sess_state{is_files_active=true,
+ files_pid=FilesPid}) ->
+ cascadae_files:deactivate(FilesPid),
+ State#sess_state{is_files_active=false};
+check_file_visibility(State) ->
+ State.
Please sign in to comment.
Something went wrong with that request. Please try again.