/// Author: Joe Audette
/// Created: 2007-06-16
/// Last Modified: 2007-11-23
///
/// The use and distribution terms for this software are covered by the
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
using System;
using System.Collections.Generic;
using System.Data;
using System.Net.Sockets;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using mojoPortal.Business;
using mojoPortal.Web;
using anmar.SharpMimeTools;
using Resources;
using SiteOffice.Business.ExternalMail;
namespace SiteOffice.ExternalMail{
public partial class PopMailboxUI : System.Web.UI.UserControl
{
#region OnInit
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new EventHandler(Page_Load);
this.btnPopInBoxRefresh.Click += new EventHandler(btnPopInBoxRefresh_Click);
}
#endregion
private static readonly ILog log
= LogManager.GetLogger(typeof(PopMailboxUI));
private SiteUser siteUser;
private int pageNumber = 1;
private int pageSize = 15;
private int totalPages = 0;
protected string sort = "Date";
private string dbName = "dbUser";
//example message id: 20070624231642.EE2301999E2@chardis.provo.novell.com
protected void Page_Load(object sender, EventArgs e)
{
// not implemented in all dbs yet
//LoadSettings();
//PopulateLabels();
//SetupScripts();
//if (!IsPostBack)
//{
// BindEmailAccountList();
//}
}
private void SetupScripts()
{
SetupInlcudeScripts();
SetupMainScripts();
// SetupInitScripts();
}
private void SetupMainScripts()
{
StringBuilder dbScript = new StringBuilder();
dbScript.Append("<script language='javascript' type='text/javascript'>\n<!--\n");
// global javascript vars
dbScript.Append("\n var db;");
dbScript.Append("\n var timerId;");
dbScript.Append("\n var timeout = 2000; "); //2 seconds
dbScript.Append("\n var spanStatus = document.getElementById('" + lblStatus.ClientID + "'); ");
//dbScript.Append("\n var queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("\n var outputDiv = document.getElementById('" + pnlOutput.ClientID + "'); ");
dbScript.Append("\n var executedNoDataReturnedMessage = '" + ExternalMailResources.GearsExecutedNoDataReturnedMessage + "';");
dbScript.Append("\n var noGearsMessage = \"" + ExternalMailResources.GearsNeededMessage + "\"; ");
dbScript.Append("\n var createDBFailureMessage = \"" + ExternalMailResources.GearsCouldNotCreateDBMessage + "\"; ");
dbScript.Append("\n var executeSQLFailureMessage = \"" + ExternalMailResources.GearsExecuteSQLFailureMessage + "\"; ");
dbScript.Append("\n var couldNotRetrieveQueryMessage = \"" + ExternalMailResources.GearsCouldNotRetrieveQueryMessage + "\"; ");
dbScript.Append("\n var noQuerySelectedMessage = \"" + ExternalMailResources.GearsNoQuerySelectedMessage + "\"; ");
dbScript.Append("\n var couldNotSaveQueryMessage = \"" + ExternalMailResources.GearsCouldNotSaveQueryMessage + "\"; ");
dbScript.Append("\n var couldNotDeleteQueryMessage = \"" + ExternalMailResources.GearsCouldNotDeleteQueryMessage + "\"; ");
dbScript.Append("\n var noTableSelectedMessage = \"" + ExternalMailResources.GearsNoTableSelectedMessage + "\"; ");
//dbScript.Append("\n ensureGearsDB();");
dbScript.Append("\n scheduleFunction(timerId,timeout,doSetup);");
WriteEnsureGearsDBFunction(dbScript);
WriteDoSetupFunction(dbScript);
WriteGetHideGearsLinkFunction(dbScript);
WriteSyncMailHandlerFunction(dbScript);
WriteSyncMailToDBFunction(dbScript);
//WriteExecuteSqlFunction(dbScript);
//WriteGetControlReferencesFunction(dbScript);
dbScript.Append("\n//-->\n</script>");
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_dbAdmin",
dbScript.ToString());
}
private void WriteSyncMailToDBFunction(StringBuilder dbScript)
{
dbScript.Append("function syncMailToDB() { ");
dbScript.Append("var mailbox = new PopToJSON(); ");
dbScript.Append("var ddInboxList = document.getElementById('" + ddInboxList.ClientID + "'); ");
dbScript.Append("var accountID; ");
dbScript.Append("for (i = 0; i < ddInboxList.length; i++) { ");
dbScript.Append("if (ddInboxList[i].selected) { ");
dbScript.Append("accountID = ddInboxList[i].value; ");
dbScript.Append("} } ");
//dbScript.Append("var accountID = \"25697cea-0756-4d35-8eda-f9f8e889d08c\"; ");
dbScript.Append("var pageNumber = 1; ");
dbScript.Append("var pageSize = 10; ");
dbScript.Append("mailbox.GetPopMail(accountID,pageNumber,pageSize,syncMailHandler); ");
//dbScript.Append("mailbox.GetCurrentUser(syncMailHandler); ");
dbScript.Append(" } ");
}
private void WriteSyncMailHandlerFunction(StringBuilder dbScript)
{
dbScript.Append("function syncMailHandler(response) { ");
//dbScript.Append("var mailbox = new PopToJSON(); ");
//dbScript.Append("var accountID = '25697cea-0756-4d35-8eda-f9f8e889d08c'; ");
//dbScript.Append("alert(response.result); ");
dbScript.Append("if (response.error != null) throw response.error; ");
dbScript.Append("var messages = eval('(' + response.result + ')'); ");
//dbScript.Append("var message = eval('(' + messages[0] + ')'); ");
dbScript.Append("alert(messages[0].MessageID); ");
dbScript.Append("alert(messages[1].MessageID); ");
dbScript.Append("alert(messages[2].MessageID); ");
dbScript.Append("alert(messages[3].MessageID); ");
//dbScript.Append("alert(mailbox.GetCurrentUser()); ");
dbScript.Append(" } ");
}
private void WriteDoSetupFunction(StringBuilder dbScript)
{
dbScript.Append("function doSetup() { ");
dbScript.Append("\n spanGears = document.getElementById('" + spnGearsLink.ClientID + "'); ");
dbScript.Append("\n spanStatus = document.getElementById('" + lblStatus.ClientID + "'); ");
//dbScript.Append("\n queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
dbScript.Append("\n outputDiv = document.getElementById('" + pnlOutput.ClientID + "'); ");
dbScript.Append("if (!window.google || !google.gears) ");
dbScript.Append("{ ");
//dbScript.Append("outputDiv.innerHTML = noGearsMessage; ");
dbScript.Append(" ");
dbScript.Append("cancelSchedule(timerId); ");
dbScript.Append("return; ");
dbScript.Append("} ");
dbScript.Append("ensureGearsDB(); ");
//dbScript.Append("populateLists(); ");
dbScript.Append("cancelSchedule(timerId); ");
dbScript.Append("hideGearsLink(); ");
dbScript.Append(" } ");
}
private void WriteEnsureGearsDBFunction(StringBuilder dbScript)
{
dbScript.Append("function ensureGearsDB() { ");
//dbScript.Append("scheduleFunction(timerId,timeout,getControls); ");
dbScript.Append("if (!window.google || !google.gears) ");
dbScript.Append("{ ");
//dbScript.Append("outputDiv.innerHTML = noGearsMessage; ");
dbScript.Append(" ");
dbScript.Append("return; ");
dbScript.Append("} ");
dbScript.Append("try { ");
// Open this page's local database.
dbScript.Append("db = new GearsDB('" + dbName + "'); ");
dbScript.Append("db.run('create table if not exists savedqueries (' + ");
dbScript.Append("'id integer not null primary key autoincrement,' + ");
dbScript.Append("'name varchar(255),' + ");
dbScript.Append("'query text)'); ");
dbScript.Append(" ");
dbScript.Append(" ");
dbScript.Append(" ");
//dbScript.Append("alert('loaded'); ");
dbScript.Append("} catch (ex) { ");
dbScript.Append("setError(spanStatus, createDBFailureMessage + ex.message); } ");
dbScript.Append(" } ");
}
//private void WriteExecuteSqlFunction(StringBuilder dbScript)
//{
// dbScript.Append("function executeSql() { ");
// dbScript.Append("if (!google.gears.factory || !db) { return; } ");
// dbScript.Append("var query = queryTextArea.value; ");
// dbScript.Append("var args = []; ");
// dbScript.Append("try { ");
// dbScript.Append("if(query.length > 0) ");
// dbScript.Append("{ ");
// dbScript.Append("var rs = db.run(query, args); ");
// dbScript.Append("printRecordset(rs, outputDiv, executedNoDataReturnedMessage); ");
// dbScript.Append("} ");
// dbScript.Append("} catch (ex) { ");
// dbScript.Append("setError(spanStatus, executeSQLFailureMessage + ex.message); } ");
// dbScript.Append(" } ");
//}
//private void WriteGetControlReferencesFunction(StringBuilder dbScript)
//{
// dbScript.Append("function getControls() { ");
// dbScript.Append("\n spanStatus = document.getElementById('" + lblStatus.ClientID + "'); ");
// //dbScript.Append("\n queryTextArea = document.getElementById('" + txtQuery.ClientID + "'); ");
// dbScript.Append("\n outputDiv = document.getElementById('" + pnlOutput.ClientID + "'); ");
// dbScript.Append("if (!window.google || !google.gears) ");
// dbScript.Append("{ ");
// dbScript.Append("outputDiv.innerHTML = noGearsMessage; ");
// dbScript.Append(" ");
// dbScript.Append("cancelSchedule(timerId); ");
// dbScript.Append("return; ");
// dbScript.Append("} ");
// //dbScript.Append("populateLists(); ");
// dbScript.Append("cancelSchedule(timerId); ");
// dbScript.Append(" } ");
//}
private void WriteGetHideGearsLinkFunction(StringBuilder dbScript)
{
dbScript.Append("function hideGearsLink() { ");
dbScript.Append("\n spanGears = document.getElementById('" + spnGearsLink.ClientID + "'); ");
dbScript.Append("if (window.google && google.gears) ");
dbScript.Append("{ ");
dbScript.Append("spanGears.style.display = 'none'; ");
dbScript.Append(" ");
dbScript.Append("} ");
dbScript.Append(" } ");
}
private void SetupInlcudeScripts()
{
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"firebug", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/firebug/firebug.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_init", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_init.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_utils", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_utils.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"gears_db", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/google/gears/gears_db.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"jayrockjson", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/ClientScript/jayrock/json.js") + "\"></script>");
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"PopToJSONproxy", "\n<script type=\"text/javascript\" src=\""
+ ResolveUrl("~/Services/PopToJSON.ashx?proxy") + "\"></script>");
}
private void BindEmailAccountList()
{
if (siteUser != null)
{
// TODO: remove try catch, after implemented in all dbs
try
{
IDataReader reader = UserEmailAccount.GetUserEmailAccountByUser(siteUser.UserGuid);
ddInboxList.DataSource = reader;
ddInboxList.DataBind();
reader.Close();
}
catch { }
}
//ListItem listItem = new ListItem();
//listItem.Value = String.Empty;
//listItem.Text = Resource.SiteOfficeNewEmailAccount;
//ddEmailAccounts.Items.Insert(0, listItem);
}
void btnPopInBoxRefresh_Click(object sender, EventArgs e)
{
//BindPopInbox("Date DESC");
}
private void BindPopInbox(String sortExpression)
{
lblPop3Error.Text = String.Empty;
// TODO: remove try catch, after implemented in all dbs
try
{
if (ddInboxList.SelectedValue.Length == 36)
{
UserEmailAccount userEmailAccount = new UserEmailAccount(new Guid(ddInboxList.SelectedValue));
EmailAccountHelper emailAccount
= new EmailAccountHelper(
userEmailAccount.UserName,
userEmailAccount.Password,
userEmailAccount.Pop3Server,
userEmailAccount.Pop3Port);
try
{
List<SharpMessage> inboxMessages
= emailAccount.GetPage(
pageNumber,
pageSize,
out totalPages);
//Comparison<Message> sortComparer;
//switch (sort)
//{
// case "Date":
// sortComparer
// = new Comparison<Message>(Message.CompareByDate);
// break;
// case "Subject":
// sortComparer
// = new Comparison<Message>(Message.CompareBySubject);
// break;
// case "From":
// default:
// sortComparer
// = new Comparison<Message>(Message.CompareByFrom);
// break;
//}
//if (sortComparer != null)
//{
// inboxMessages.Sort(sortComparer);
//}
//rptPopMail.DataSource = inboxMessages;
//rptPopMail.DataBind();
grdMail.DataSource = inboxMessages;
grdMail.DataBind();
}
catch (SocketException ex)
{
if (log.IsErrorEnabled)
{
log.Error("SiteOfficeHome.BindPopInbox", ex);
}
lblPop3Error.Text = "Sorry there was a problem connecting. Please check your account settings for this account. "
+ ex.Message;
}
}
}
catch (Exception exx)
{
lblPop3Error.Text = exx.Message;
}
}
void rptPopMail_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "view":
String messageID = e.CommandArgument.ToString();
break;
}
}
void lnkSortSubject_Click(object sender, EventArgs e)
{
//BindPopInbox("Subject");
}
void lnkSortDate_Click(object sender, EventArgs e)
{
//BindPopInbox("Date DESC");
}
void lnkSortFrom_Click(object sender, EventArgs e)
{
//BindPopInbox("From");
}
private void PopulateLabels()
{
btnTest.Attributes.Add("OnClick", "syncMailToDB(); return false;");
litGearsInfo.Text = ExternalMailResources.GearsNeededMessage;
btnPopInBoxRefresh.Text = "Check Mail";
//lnkSortFrom.Text = "From";
//lnkSortSubject.Text = "Subject";
//lnkSortDate.Text = "Date";
}
private void LoadSettings()
{
siteUser = SiteUtils.GetCurrentSiteUser();
dbName = "db" + siteUser.UserGuid.ToString().Replace("-", string.Empty);
}
//private void SetupScripts()
//{
// this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "dojo", "\n<script type=\"text/javascript\" src=\""
// + ResolveUrl("~/ClientScript/dojo0-9/dojo/dojo.js") + "\" djConfig=\"parseOnLoad: true\"></script>");
// this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "dojo-offline", "\n<script type=\"text/javascript\" src=\""
// + ResolveUrl("~/ClientScript/dojo0-9/dojox/off/offline.js") + "\"></script>");
// StringBuilder dojoRequireScript = new StringBuilder();
// dojoRequireScript.Append("<script language='javascript' type='text/javascript'>\n<!--\n");
// dojoRequireScript.Append("\n dojo.require('dojo.parser'); ");
// dojoRequireScript.Append("\n dojox.off.ui.appName = 'Site Office'; ");
// //dojoRequireScript.Append("\n dojox.off.files.cache('"
// // + ResolveUrl("~/Data/SiteImages/office/SQL.png") + "'); ");
// //dojoRequireScript.Append("\n dojox.off.files.cache('"
// // + ResolveUrl("~/ClientScript/dojo0-9/dojox/off/offline.js") + "'); ");
// //dojoRequireScript.Append("\n dojox.off.files.cache('"
// // + ResolveUrl(Request.RawUrl) + "'); ");
// dojoRequireScript.Append(" ");
// dojoRequireScript.Append(" ");
// dojoRequireScript.Append("dojox.off.initialize(); ");
// dojoRequireScript.Append("\n//-->\n</script>");
// // string dojoRequireScript = @"
// //<script language='javascript' type='text/javascript'>
// //<!--
// //
// // dojo.require('dojo.parser');
// // // set our application name
// // dojox.off.ui.appName = 'Site Office';
// // dojox.off.files.cache([
// // 'images/file1.png',
// // 'images/file2.png,
// // 'scripts/some_dynamic_script_tag.js'
// // ]);
// //
// // dojox.off.initialize();
// //
// ////-->
// //</script>";
// Page.ClientScript.RegisterClientScriptBlock(
// this.GetType(),
// "dojo-require",
// dojoRequireScript.ToString());
//}
}
}
|