Skip to content

Commit

Permalink
Feed.vala documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
war1025 committed Jul 8, 2014
1 parent 21c524a commit 9c222a8
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions feed.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ using Soup;

namespace GmailFeed {

/**
* Fields that we store using libsecret.
**/
public class OAuthFields {
public const string CLIENT_ID = "client_id";
public const string CLIENT_SECRET = "client_secret";
Expand All @@ -11,6 +14,15 @@ public class OAuthFields {
public const string REFRESH_TOKEN = "refresh_token";
}

/**
* Possible errors that feed actions can return.
*
* SUCCESS : No Error.
* UNKNOWN : Failed, likely due to a network error
* NEED_TOKEN : The bearer token is invalid / needs to be refreshed
* INVALID_AUTH : The refresh token is invalid
* NEED_OAUTH_ID : The OAuth info is missing or invalid.
**/
public enum AuthError {
SUCCESS,
UNKNOWN,
Expand All @@ -22,6 +34,7 @@ public enum AuthError {
public delegate AuthError AuthCodeDelegate(string authCode);

public class Feed : Object {
//{ Signals
public signal void newMessage(GMessage msg);
public signal void updatedMessage(GMessage msg);
public signal void messageStarred(string id);
Expand Down Expand Up @@ -54,11 +67,11 @@ public class Feed : Object {
* @param result: The error that the update completed with.
**/
public signal void updateComplete(AuthError result);
//}

private delegate void SuccessSignal(string msgId);

public string address {get; private set;}

//{ Members
private string? clientId;
private string? clientSecret;
private string? redirectUri;
Expand All @@ -70,6 +83,10 @@ public class Feed : Object {
private Secret.Schema schema;

private Gee.Map<string, GMessage> messages;
//}

//{ Properties
public string address {get; private set;}

public Gee.Collection<GMessage> inbox {
owned get {
Expand All @@ -82,6 +99,7 @@ public class Feed : Object {
return this.messages.size;
}
}
//}

public Feed(string address) {
this.address = address;
Expand All @@ -95,6 +113,9 @@ public class Feed : Object {
this.messages = new Gee.HashMap<string, GMessage>();
}

/**
* Load any stored info for this Feed's address out of libsecret
**/
public void loadInfo() {
this.bearerToken = Secret.password_lookup_sync(this.schema, null,
"address", this.address,
Expand Down Expand Up @@ -130,6 +151,9 @@ public class Feed : Object {
this.session.timeout = 15;
}

/**
* Set the OAuth information for this address.
**/
public AuthError setOAuthInfo(string clientId, string clientSecret, string redirectUri) {
var success = Secret.password_store_sync(this.schema, Secret.COLLECTION_DEFAULT,
"%s client ID".printf(this.address),
Expand Down Expand Up @@ -164,9 +188,11 @@ public class Feed : Object {
}
}

/**
* Build the URL that the user should go to in order
* to authorize us to use the Gmail API.
**/
public string? getAuthUrl() {
this.create_session();

if(!this.hasOAuthId()) {
return null;
}
Expand All @@ -189,7 +215,13 @@ public class Feed : Object {
return auth_uri.to_string(false);
}

/**
* Set the authorization code that we got back from the AuthUrl.
* This allows us to retrieve a Bearer and Refresh token.
**/
public AuthError setAuthCode(string authCode) {
this.create_session();

var token_addr = "https://accounts.google.com/o/oauth2/token";
var token_msg = new Message("POST", token_addr);

Expand Down Expand Up @@ -251,6 +283,9 @@ public class Feed : Object {
return AuthError.SUCCESS;
}

/**
* Use our refresh token, if we have one, to retrieve a new bearer token.
**/
public AuthError refreshBearerToken() {
this.create_session();

Expand Down Expand Up @@ -311,6 +346,9 @@ public class Feed : Object {
return AuthError.NEED_TOKEN;
}

/**
* Attempt to update the feed, handling various error cases along the way if we can.
**/
public AuthError update() {
AuthError result;

Expand Down Expand Up @@ -343,6 +381,9 @@ public class Feed : Object {
return result;
}

/**
* Refresh our inbox so that it contains the current unread emails.
**/
public AuthError refreshInbox() {
if(!this.hasBearerToken()) {
return AuthError.NEED_TOKEN;
Expand Down Expand Up @@ -429,6 +470,7 @@ public class Feed : Object {
return AuthError.SUCCESS;
}

//{ Message modifiers
public AuthError markRead(string msgId) {
return this.modify(msgId, {}, {"UNREAD"}, (id) => {this.messageRead(id);});
}
Expand Down Expand Up @@ -510,6 +552,7 @@ public class Feed : Object {
successSignal(msgId);
return AuthError.SUCCESS;
}
//}

private string buildUrl(string end) {
var base_addr = "https://www.googleapis.com/gmail/v1/users/me%s";
Expand Down

0 comments on commit 9c222a8

Please sign in to comment.