using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace GoogleControl2 { /// /// Example of ASCX (ASP.NET) control. /// Calls the Google Web API. ///

Copyright (c) 2002 Alexis Grandemange
/// Mail: alexis.grandemange@pagebox.net

///
This program is free software; you can redistribute it and/or
	/// modify it under the terms of the GNU Lesser General Public
	/// License as published by the Free Software Foundation; version
	/// 2.1 of the License.
	/// This library is distributed in the hope that it will be useful,
	/// but WITHOUT ANY WARRANTY; without even the implied warranty of
	/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	/// GNU Lesser General Public License for more details.
	/// A copy of the GNU Lesser General Public License lesser.txt should be
	/// included in the distribution.
///
public class GoogleControl : System.Web.UI.UserControl { protected System.Web.UI.WebControls.Panel GooglePanel; protected System.Web.UI.WebControls.Label title; protected System.Web.UI.WebControls.TextBox term; protected System.Web.UI.WebControls.Button search; protected System.Web.UI.WebControls.Button clear; protected System.Web.UI.WebControls.DataGrid result; protected System.Web.UI.WebControls.DataGrid detail; protected System.Web.UI.WebControls.Label labStatus; protected System.Web.UI.WebControls.Label copyright; /// /// CSS class to use for the title /// private string titleCss = null; public string TitleCss { get { return titleCss; } set { titleCss = value; } } /// /// CSS class to use for the status /// private string statusCss = null; public string StatusCss { get { return statusCss; } set { statusCss = value; } } /// /// CSS class to use for the copyright /// private string copyrightCss = null; public string CopyrightCss { get { return copyrightCss; } set { copyrightCss = value; } } /// /// Table min width (when the Datagrids are not displayed) /// private string minWidth = null; public string MinWidth { get { return minWidth; } set { minWidth = value; } } /// /// Table max width (when the Datagrids are displayed) /// private string maxWidth = null; public string MaxWidth { get { return maxWidth; } set { maxWidth = value; } } /// /// CSS class to use for the Datagrid header /// private string dgHeaderCss = null; public string DgHeaderCss { get { return dgHeaderCss; } set { dgHeaderCss = value; } } /// /// CSS class to use for the Datagrid items /// public string DatagridCss { get { return dgCss; } set { dgCss = value; } } private string dgCss = null; /// /// CSS class to use for the Datagrid alternate items /// private string dgAltCss = null; public string DgAltCss { get { return dgAltCss; } set { dgAltCss = value; } } /// /// CSS class to use for the embedding panel /// private string css = null; public string Css { get { return css; } set { css = value; } } /// /// Google license /// private string license = null; public string License { get { return license; } set { license = value; } } /// /// Array bound with the result Datagrid. /// Contains ResultItem objects. /// ArrayList results; /// /// Set by bind() and used by OnSearch(). /// Used to display Web service invocation errors. /// string bindError = null; public GoogleControl() { this.Init += new System.EventHandler(Page_Init); } /// /// Set the CSS classes and width from the properties. /// private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { title.CssClass = titleCss; copyright.CssClass = copyrightCss; labStatus.CssClass = statusCss; result.HeaderStyle.CssClass = dgHeaderCss; result.ItemStyle.CssClass = dgCss; result.AlternatingItemStyle.CssClass = dgAltCss; detail.HeaderStyle.CssClass = dgHeaderCss; detail.ItemStyle.CssClass = dgCss; detail.AlternatingItemStyle.CssClass = dgAltCss; GooglePanel.CssClass = css; GooglePanel.Width = Unit.Pixel(Convert.ToInt16(minWidth)); } } private void Page_Init(object sender, EventArgs e) { InitializeComponent(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); search.Click += new System.EventHandler(OnSearch); clear.Click += new System.EventHandler(OnClear); result.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(dgResult_Select); } #endregion /// /// Called when the user clicks on the Search button. /// Calls bind. /// In case of success, binds the result Datagrid and set its header, /// item and alternate item CSS classes. /// private void OnSearch(object sender, System.EventArgs e) { if (term.Text.Length == 0) labStatus.Text = "You must set the search string"; else { unbindDetail(); int resultNb = bind(); if (resultNb == -1) labStatus.Text = bindError; else { GooglePanel.Width = Unit.Pixel(Convert.ToInt16(maxWidth)); result.DataSource = results; result.DataBind(); labStatus.Text = "Found " + resultNb + " entries"; } } } /// /// Called when the user clicks on a Sel button in the result Datagrid. /// Binds the detail Datagrid and set its header, /// item and alternate item CSS classes. /// private void dgResult_Select(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { int sel = e.Item.ItemIndex; if (sel != -1) { string Key = (string)result.DataKeys[sel]; string[] fields = Key.Split(new char[]{ '|' }); ArrayList al = new ArrayList(); DetailItem di = new DetailItem("Summary", fields[0]); al.Add(di); di = new DetailItem("URL", fields[1]); al.Add(di); di = new DetailItem("Snippet", fields[2]); al.Add(di); di = new DetailItem("Title", fields[3]); al.Add(di); di = new DetailItem("Dir title", fields[5]); al.Add(di); di = new DetailItem("Cache size", fields[4]); al.Add(di); detail.DataSource = al; detail.DataBind(); GooglePanel.Width = Unit.Pixel(Convert.ToInt16(maxWidth)); } } /// /// Called when the user clicks on the Clear button. /// Shrinks the control (clear the Datagrids). /// private void OnClear(object sender, System.EventArgs e) { unbindDetail(); result.DataSource = null; result.DataBind(); GooglePanel.Width = Unit.Pixel(Convert.ToInt16(minWidth)); } /// /// Calls the Google Web service and populates the result array list. /// Supports a special license, offline. /// If you don't the license in GoogleControl.xml or if you don't define /// GoogleControl.xml, the Web service is not called and results is populated /// with dummy entries. Useful for offline tests. /// /// Number of entries found private int bind() { if (license.Equals("offline")) { results = new ArrayList(10); for (int i = 0; i < 10; ++ i) { string idx = Convert.ToString(i); ResultItem resi = new ResultItem("summary" + idx, "http://localhost/home2/home.html", "snippet" + idx, "title" + idx, idx, "Directory Title" + idx); results.Add(resi); } return 10; } GoogleSearchService gss = new GoogleSearchService(); try { // gss.Discover(); GoogleSearchResult gsr = gss.doGoogleSearch(license, term.Text, 0, 10, true, "", true, "", "", ""); int resultNb = gsr.resultElements.Length; results = new ArrayList(resultNb); IEnumerator ie = gsr.resultElements.GetEnumerator(); while(ie.MoveNext()) { ResultElement re = (ResultElement)ie.Current; ResultItem resi = new ResultItem(re.summary, re.URL, re.snippet, re.title, re.cachedSize, re.directoryTitle); results.Add(resi); } return resultNb; } catch(Exception e) { bindError = e.Message; } return -1; } /// /// Clear the detail Datagrid. /// private void unbindDetail() { detail.DataSource = null; detail.DataBind(); } } /// /// Describes a row of the result Datagrid. /// public sealed class ResultItem { /// /// Summary info returned by the Google Web service. /// public string summary; /// /// URL returned by the Google Web service. /// public string url; /// /// Snippet returned by the Google Web service. /// public string snippet; /// /// Title returned by the Google Web service. /// public string title; /// /// Cached size returned by the Google Web service. /// public string cachedSize; /// /// Title of the page in a directory returned by the Google Web service. /// public string directoryTitle; /// /// Constructor. Set the member fields. /// /// Summary info returned by the Google Web service /// URL returned by the Google Web service /// Snippet returned by the Google Web service /// Title returned by the Google Web service /// Cached size returned by the Google Web service /// Title of the page in a directory /// returned by the Google Web service public ResultItem(string summary, string URL, string snippet, string title, string cachedSize, string directoryTitle) { this.summary = summary; this.url = URL; this.snippet = snippet; this.title = title; this.cachedSize = cachedSize; this.directoryTitle = directoryTitle; } /// /// URL property. Displayed with a hyperlink. /// public string URL { get { return url; } } /// /// Title property. Displayed on a column. /// public string Title { get { return title; } } /// /// Key property. Contains all fields. Used to display details. /// public string Key { get { return summary + '|' + url + '|' + snippet + '|' + title + '|' + cachedSize + "|" + directoryTitle; } } } /// /// Describes a row in the detail Datagrid. /// public sealed class DetailItem { /// /// Name of a field - for instance Summary. /// private string name; /// /// Value of a field. /// private string val; /// /// Constructor. Set the member variables. /// /// Name of a field /// Value of a field public DetailItem(string name, string val) { this.name = name; this.val = val; } /// /// Returns the name of a field. /// public string Name { get { return name; } } /// /// Returns the value of a field, /// public string Value { get { return val; } } } }