Index: droogle/css/droogle.css
===================================================================
--- droogle/css/droogle.css	(revision 1430)
+++ droogle/css/droogle.css	(working copy)
@@ -9,4 +9,7 @@
 }
 .presentation {
   list-style-image:url('images/page_white_powerpoint.png');
+}
+.pdf {
+	list-style-image:url('images/page_white_acrobat.png');
 }
\ No newline at end of file
Index: droogle/droogle.module
===================================================================
--- droogle/droogle.module	(revision 1430)
+++ droogle/droogle.module	(working copy)
@@ -1,522 +1,689 @@
-<?php
-
-/**
- * @file
- * A module to integrate Drupal with Google Drive.
- * Created by:  James Barnett, Babson College 2014.
- */
-
-/**
- * Implements hook_help().
- */
-function droogle_help($path, $arg) {
-  $output = '';
-  switch ($path) {
-    case "admin/help#droogle":
-      $output = '<p>' . t("The module connects Drupal to Google Drive") . '</p>';
-      break;
-  }
-  return $output;
-}
-
-/**
- * Implements hook_permission().
- */
-function droogle_permission() {
-  return array(
-    'administer droogle' => array(
-      'title' => t('administer droogle'),
-      'description' => t('administer droogle'),
-    ),
-    'upload google docs' => array(
-      'title' => t('upload google docs'),
-      'description' => t('upload google docs'),
-    ),
-    'view google docs' => array(
-      'title' => t('view google docs'),
-      'description' => t('view google docs'),
-    ),
-  );
-}
-
-/**
- * Implements hook_menu().
- */
-function droogle_menu() {
-  $items = array();
-  $items['droogle_gdrive_create_file'] = array(
-    'title' => 'Gdrive File Upload',
-    'page callback' => 'droogle_file_upload',
-    'access arguments' => array('access content'),
-  );
-  $items['droogle'] = array(
-    'title' => 'droogle',
-    'page callback' => 'droogle_list_files',
-    'access arguments' => array('view google docs'),
-  );
-  $items['admin/settings/droogle'] = array(
-    'title' => 'Droogle Settings',
-    'description' => 'Configuration of who can upload to google docs from an OG droogle block',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('droogle_settings_form'),
-    'access arguments' => array('administer droogle'),
-  );
-  $items['droogle_get_google_token'] = array(
-    'title' => 'droogle',
-    'page callback' => 'droogle_get_google_token',
-    'access arguments' => array('administer droogle'),
-  );
-  
-
-  return $items;
-}
-
-/**
- * Uses the form api for an admin settings form.
- */
-function droogle_settings_form() {
-  global $user;
-  $droogle_clientid = variable_get('droogle_clientid', '');
-  $droogle_secret = variable_get('droogle_secret', '');
-  $droogle_title_text = variable_get('droogle_title_text', '');
-  $droogle_password = variable_get('droogle_password', '');
-  $droogle_refresh_token = variable_get('droogle_refresh_token', '');
-  $droogle_og_clientid_cck_field = variable_get('droogle_og_cck_field_gdrive_client_id');
-  $droogle_og_clientsecret_cck_field = variable_get('droogle_og_cck_field_gdrive_client_secret');
-  $droogle_default_domain = variable_get('droogle_default_domain', '');
-
-  $form['droogle_default_domain'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Enter the default domain to use to form links to your google docs ie: babson.edu, if your email address is simply yourname@gmail.com then do not worry about this, but if you are using a google education or business account and your gmail address is yourname@babson.edu for instance then enter a value for this field'),
-    '#default_value' => $droogle_default_domain,
-    '#size' => 60,
-    '#maxlength' => 64,
-    '#description' => t('the default domain to use in creating links to your google docs'),
-  );
-  $form['sitewide_nonog'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Settings controlling the non Organic Groups context for Droogle'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['sitewide_nonog']['droogle_title_text'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Enter the title to put at the top of the droogle page (when not within an Organic Groups context)'),
-    '#default_value' => $droogle_title_text,
-    '#size' => 60,
-    '#maxlength' => 64,
-    '#description' => t('the non OG title of the droogle page, default is:  "DROOGLE: A list of your google docs"'),
-  );
-  $form['sitewide_nonog']['droogle_clientid'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Enter the google client id to use (when not within an Organic Groups context)
-                  -- you can generate a client id at https://cloud.google.com/console'),
-    '#default_value' => $droogle_clientid,
-    '#size' => 100,
-    '#maxlength' => 150,
-    '#description' => t('the non OG google client id'),
-  );
-  $form['sitewide_nonog']['droogle_secret'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Enter the google client secret to use (when not within an Organic Groups context)
-                  -- you can generate a client secret at https://cloud.google.com/console'),
-    '#default_value' => $droogle_secret,
-    '#size' => 60,
-    '#maxlength' => 64,
-    '#description' => t('the non OG google client secret'),
-  );
-  $form['sitewide_nonog']['droogle_refresh_token'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Enter the google refresh token, generate one by going to
-      the url http://yoururl.com/droogle_get_google_token but MAKE SURE TO ENABLE THE DEVEL MODULE FIRST,
-      you can disable the devel module after you generate the refresh token, ignore the error and
-      copy and paste the refesh token generated (not the access code) into this text box'),
-    '#default_value' => $droogle_refresh_token,
-    '#size' => 60,
-    '#maxlength' => 64,
-    '#description' => t('the google refresh token so the login to google does not timeout'),
-  );
-  if (module_exists('og')) {
-    $form['single_groups'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Overriding google username and password for individual Organic Group(s)'),
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
-    );
-    $form['single_groups']['droogle_og_cck_field_gdrive_client_id'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Enter the "group" content types cck field machine name to use to override
-                    a particular groups google client id)'),
-      '#default_value' => $droogle_og_clientid_cck_field,
-      '#size' => 60,
-      '#maxlength' => 64,
-      '#description' => t('the OG group cck field to use to set a particular OG droogle password
-                          -- you will have to setup a new cck field for the "group" content type
-                          matching the machine name set on this page'),
-    );
-    $form['single_groups']['droogle_og_cck_field_gdrive_client_secret'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Enter the "group" content types cck field machine name to use to override
-                    a particular groups google client secret -- this is optional and if the field
-                    is left empty when configuring a particular group, the global Droogle organic
-                    groups settings will be followed)'),
-      '#default_value' => $droogle_og_clientsecret_cck_field,
-      '#size' => 60,
-      '#maxlength' => 64,
-      '#description' => t('the OG group cck field to use to set a particular OG droogle password
-                          -- you will have to setup a new cck field for the "group" content type
-                          matching the machine name set on this page'),
-    );
-  }
-    $form = system_settings_form($form);
-    $form['#submit'][] = 'droogle_settings_form_submit';
-    return $form;
-}
-
-/**
- * Form submission handler droogle_settings_form().
- */
-function droogle_settings_form_submit($form, &$form_state) {
-  
-}
-
-function droogle_get_google_token() {
-  $url = "https://accounts.google.com/o/oauth2/auth";
-  $client_id = variable_get('droogle_clientid', '');
-  $client_secret = variable_get('droogle_secret', '');
-  $redirect_uri = "http://" . $_SERVER['HTTP_HOST'] . "/droogle_get_google_token";
-  $access_type = "offline";
-  $approval_prompt = "force";
-  $grant_type = "authorization_code";
-  $scope = "https://www.googleapis.com/auth/drive";
-
-
-  $params_request = array(
-    "response_type" => "code",
-    "client_id" => "$client_id",
-    "redirect_uri" => "$redirect_uri",
-    "access_type" => "$access_type",
-    "approval_prompt" => "$approval_prompt",
-    "scope" => "$scope"
-    );
-
-  $request_to = $url . '?' . http_build_query($params_request);
-
-  if(isset($_GET['code'])) {
-    // try to get an access token
-    $code = $_GET['code'];
-    $url = 'https://accounts.google.com/o/oauth2/token';
-    $params = array(
-        "code" => $code,
-        "client_id" => "$client_id",
-        "client_secret" => "$client_secret",
-        "redirect_uri" => "$redirect_uri",
-        "grant_type" => "$grant_type"
-    );
-
-    $curl = curl_init($url);
-    curl_setopt($curl, CURLOPT_POST, true);
-    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
-    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
-    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
-    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);	
-
-    $json_response = curl_exec($curl);
-    curl_close($curl);
-
-    $authObj = json_decode($json_response);
-    $result = array();
-    $result['refresh_token'] = $authObj->refresh_token;
-    $result['access_token'] = $authObj->access_token;
-
-}
-
-header("Location: " . $request_to);
-dpm($result);
-return 'refesh token is ' . $result['refresh_token'];
-  
-}
-
-/**
- *  Implements hook_block_info().
- */
-function droogle_block_info() {
-  $blocks = array();
-  $blocks['droogle-og-block'] = array(
-    'info' => t('Droogle OG Block'),
-  );
-  return $blocks;
-}
-
- /**
-  *  Implements hook_block_view().
-  */
-function droogle_block_view($delta = '') {
-  $block = array();
-  switch ($delta) {
-    case 'droogle-og-block':
-      $block['subject'] = t('Droogle OG Block');
-      $block['content'] = droogle_list_files();
-      break;
-  }
-  return $block;
-}
-
-
-/*
- *  Function connects to google drive.
- */
-function droogle_gdrive_connect($redirect_uri = NULL, $client_id = NULL, $client_secret = NULL, $redirect_uri = NULL) {
-  watchdog('redirect_uri', 'redirect is ' . $redirect_uri);
-  if ($path = libraries_get_path('google-api-php-client-git-version')) {
-    ini_set('include_path',ini_get('include_path').':' . $_SERVER['DOCUMENT_ROOT'] . '/sites/all/libraries/google-api-php-client-git-version/src');
-    require_once 'Google/Client.php';
-    require_once 'Google/Http/MediaFileUpload.php';
-    require_once 'Google/Service/Drive.php';
-   
-  }
-  else {
-    drupal_set_message('You need to install the google api client library in the folder
-      google-api-php-client-git-version within the sites/all/libraries folder');
-  }
-
-  /************************************************
-    ATTENTION: Fill in these values! Make sure
-    the redirect URI is to this page, e.g:
-    http://localhost:8080/fileupload.php
-   ************************************************/
-  if (module_exists('og_context')) {
-    $group_google_clientid_cck = 'field_' . variable_get('droogle_og_cck_field_gdrive_client_id');
-    $group_google_clientsecret_cck = 'field_' . variable_get('droogle_og_cck_field_gdrive_client_secret');
-    $current_group = og_context();
-    if (isset($current_group->etid)) {
-      $group_node = node_load($current_group->etid);
-      $client_id = $group_node->{$group_google_clientid_cck}['und'][0]['value'];
-      $client_secret = $group_node->{$group_google_clientsecret_cck}['und'][0]['value'];
-    }
-  }
-  
-  if (!isset($droogle_clientid) || !isset($droogle_secret)) {
-    $droogle_clientid = variable_get('droogle_clientid', '');
-    $droogle_secret = variable_get('droogle_secret', '');
-  }
-  $client = new Google_Client();
-  $client->setClientId($droogle_clientid);
-  $client->setClientSecret($droogle_secret);
-  $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/droogle' . $redirect_uri);
-  $client->addScope("https://www.googleapis.com/auth/drive");
-  $service = new Google_Service_Drive($client);
-
-  if (isset($_REQUEST['logout'])) {
-    unset($_SESSION['upload_token ']);
-  }
-
-  if (isset($_GET['code'])) {
-    $client->authenticate($_GET['code']);
-    $_SESSION['upload_token'] = $client->getAccessToken();
-    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
-    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
-  }
-
-  if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
-    $client->setAccessToken($_SESSION['upload_token']);
-    if ($client->isAccessTokenExpired()) {
-      unset($_SESSION['upload_token']);
-    }
-  } else {
-    $authUrl = $client->createAuthUrl();
-  }
-  $return_result['client'] = $client;
-  $return_result['service'] = $service;
-  if (isset($authUrl)) {
-    $return_result['authUrl'] = $authUrl;
-  }
-  return $return_result;
-}
-
-/*
- *  Function creates a google drive document.
- *  This functionality works but the module will
- *  do more with it soon in coming versions.
- */
-function droogle_file_upload($doctype = 'word', $doc_title = NULL, $role = NULL, $type = NULL, $permission_values = NULL) {
-  unset($result);
-  $return_result = droogle_gdrive_connect('droogle_gdrive_create_file');
-  $client = $return_result['client'];
-  $service = $return_result['service'];
-  if (isset($return_result['authUrl'])) {
-    $authUrl = $return_result['authUrl'];
-  }
-  /************************************************
-    If we're signed in then lets try to upload our
-    file.
-   ************************************************/
-  $refresh_token = variable_get('droogle_refresh_token', '');
-  if(!empty($refresh_token) && $client->isAccessTokenExpired()) {
-    $client->refreshToken($refresh_token);
-  }
-
-  if ($client->getAccessToken()) {
-    $file = new Google_Service_Drive_DriveFile();
-    if (!isset($doc_title)) {
-      $doc_title = 'Hello world from the new api! ' . time();
-    }
-    $file->setTitle($doc_title);
-    switch ($doctype) {
-    case 'word':
-      $file->setMimeType('application/vnd.google-apps.document');
-      break;
-    case 'excel':
-      $file->setMimeType('application/vnd.google-apps.spreadsheet');
-      break;
-    case 'presentation':
-      $file->setMimeType('application/vnd.google-apps.presentation');
-      break;
-    case 'drawing':
-      $file->setMimeType('application/vnd.google-apps.drawing');
-      break;
-    }
-    
-    $result = $service->files->insert($file);
-    // Permission documentation is here:  https://developers.google.com/drive/v2/reference/permissions and
-    // here https://developers.google.com/drive/web/manage-sharing
-    $permission = new Google_Service_Drive_Permission();
-    if (isset($permission_values)) {
-      foreach ($permission_values as $value) {
-        $permission->setValue($value);
-      }
-    }
-    else {
-    $permission->setValue('wcheung@babson.edu');
-    }
-    
-    if (isset($type)) {
-      $permission->setType($type);
-    }
-    else {
-      $permission->setType('user');
-    }
-    
-    if (isset($role)) {
-      $permission->setRole($role);
-    }
-    else {
-      $permission->setRole('writer');
-    }
-  
-    $service->permissions->insert( $result['id'], $permission );
-  }
-
-  $content = '';
-  if (isset($authUrl)) {
-    $content .= '<a class="login" href=' . $authUrl . '>Connect Me!</a></div>';
-  }
-  if (isset($result) && $result) {
-   //$content .= '<div class="shortened"><pre> ' . print_r($result, TRUE) . '</pre></div></div>';
-   drupal_set_message('File was Created in Google Drive!');
-  }
-  return $content;
-}
-
-/*
- *  A page callback for when you go to the droogle url.
- */
-function droogle_list_files() {
-  $droogle_title_text = variable_get('droogle_title_text', '');
-  drupal_set_title($droogle_title_text);
-  unset($result);
-  $return_result = droogle_gdrive_connect('droogle');
-  $client = $return_result['client'];
-  $service = $return_result['service'];
-  if (isset($return_result['authUrl'])) {
-    $authUrl = $return_result['authUrl'];
-  }
-
-  /************************************************
-    If we're signed in then lets try to upload our
-    file.
-   ************************************************/
-  $refresh_token = variable_get('droogle_refresh_token', '');
-  
-  if(!empty($refresh_token) && $client->isAccessTokenExpired()) {
-    $client->refreshToken($refresh_token);
-  }
-  if ($client->getAccessToken()) {
-    $content = theme('droogle_list_files', array('service' => $service));
-  }
-  elseif (isset($authUrl)) {
-    $content = '<a class="login" href=' . $authUrl . '>Connect Me!</a></div>';
-  }
-  return $content;
-}
-
-/**
- * Implements hook_theme().
- *
- * Information on hook theme http://groups.drupal.org/node/14274#comment-57950.
- *    1. hook_theme which adds an array of callbacks and their arguments to the
- *      theme registry. I didn't realize that I had to visit the modules page to
- *      rebuild the theme registry before it would be added.
- *    2. The themable function itself which starts with theme_ followed by the
- *      function name that was added to the registry with hook_theme.
- *    3. theme("whatever_function, $whatever_argument, $whatever_argument ")
- *     which actually calls the function.
- */
-function droogle_theme() {
-  return array(
-    'droogle_list_files' => array(
-      'arguments' => array(
-        'service' => NULL,
-      ),
-    ),
-  );
-}
-
-/**
- * Implements of hook_theme().
- *
- * @param array $variables
- *   an array with google document listings.
- *
- * @return string
- *   html with listing of google docs to be output to the screen.
- */
-function theme_droogle_list_files($variables) {
-  drupal_add_css(drupal_get_path('module', 'droogle') . '/css/droogle.css');
-  $service = $variables['service'];
-  $files = $service->files->listFiles();
-  $file_list = $files->getItems();
-  $content = '<ul>';
-  // Below line if for debugging.
-  // $content .= '<div class="shortened">File List:  <pre> ' . print_r($file_list, TRUE) . '</pre></div></div>';
-  foreach ($file_list as $file) {
-    if ($file['mimeType'] == 'application/vnd.google-apps.folder' || (isset($file['explicitlyTrashed']) && $file['explicitlyTrashed'] == 1)) {
-      // Don't list folders, just files (for now). It's displaying all files in folders anyhow.
-      continue;
-    }
-    switch ($file['mimeType']) {
-      case 'application/vnd.google-apps.document':
-        $content .= '<a href="' . $file["alternateLink"] . '"><li class="word">' . $file["title"] . '</li></a>';
-        break;
-      case 'application/vnd.google-apps.spreadsheet':
-        $content .= '<a href="' . $file["alternateLink"] . '"><li class="excel">' . $file["title"] . '</li></a>';
-        break;
-      case 'application/vnd.google-apps.presentation':
-        $content .= '<a href="' . $file["alternateLink"] . '"><li class="presentation">' . $file["title"] . '</li></a>';
-        break;
-      case 'application/vnd.google-apps.drawing':
-        $content .= '<a href="' . $file["alternateLink"] . '"><li class="drawing">' . $file["title"] . '</li></a>';
-        break;
-    }
-  }
-  $content .= '</ul>';
-  
-  if (isset($result) && $result) {
-   // For debugging
-   //$content .= '<div class="shortened"><pre> ' . print_r($result, TRUE) . '</pre></div></div>';
-   
-  }
-  return $content;
-}
-
-
+<?php
+
+/**
+ * @file
+ * A module to integrate Drupal with Google Drive.
+ * Created by:  James Barnett, Babson College 2014.
+ */
+
+/**
+ * Implements hook_help().
+ */
+function droogle_help($path, $arg) {
+	$output = '';
+	switch ($path) {
+		case "admin/help#droogle" :
+			$output = '<p>' . t ( "The module connects Drupal to Google Drive" ) . '</p>';
+			break;
+	}
+	return $output;
+}
+
+/**
+ * Implements hook_permission().
+ */
+function droogle_permission() {
+	return array (
+			'administer droogle' => array (
+					'title' => t ( 'administer droogle' ),
+					'description' => t ( 'administer droogle' )
+			),
+			'upload google docs' => array (
+					'title' => t ( 'upload google docs' ),
+					'description' => t ( 'upload google docs' )
+			),
+			'view google docs' => array (
+					'title' => t ( 'view google docs' ),
+					'description' => t ( 'view google docs' )
+			)
+	);
+}
+
+/**
+ * Implements hook_menu().
+ */
+function droogle_menu() {
+	$items = array ();
+	$items ['droogle_gdrive_create_file'] = array (
+			'title' => 'Gdrive File Upload',
+			'page callback' => 'droogle_file_upload',
+			'access arguments' => array (
+					'access content'
+			)
+	);
+	$items ['droogle'] = array (
+			'title' => 'List files',
+			'page callback' => 'droogle_list_files',
+			'access arguments' => array (
+					'view google docs'
+			)
+	);
+	$items ['droogle-upload'] = array (
+			'title' => 'Upload File',
+			'page callback' => 'drupal_get_form',
+			'page arguments' => array (
+					'droogle_upload_form'
+			),
+			'access arguments' => array (
+					'upload google docs'
+			)
+	);
+	$items ['admin/settings/droogle'] = array (
+			'title' => 'Droogle Settings',
+			'description' => 'Configuration of who can upload to google docs from an OG droogle block',
+			'page callback' => 'drupal_get_form',
+			'page arguments' => array (
+					'droogle_settings_form'
+			),
+			'access arguments' => array (
+					'administer droogle'
+			)
+	);
+	$items ['droogle_get_google_token'] = array (
+			'title' => 'droogle',
+			'page callback' => 'droogle_get_google_token',
+			'access arguments' => array (
+					'administer droogle'
+			)
+	);
+	
+	return $items;
+}
+
+/**
+ * Uses the form api for an admin settings form.
+ */
+function droogle_settings_form() {
+	global $user;
+	$droogle_clientid = variable_get ( 'droogle_clientid', '' );
+	$droogle_secret = variable_get ( 'droogle_secret', '' );
+	$droogle_title_text = variable_get ( 'droogle_title_text', '' );
+	$droogle_password = variable_get ( 'droogle_password', '' );
+	$droogle_refresh_token = variable_get ( 'droogle_refresh_token', '' );
+	$droogle_og_clientid_cck_field = variable_get ( 'droogle_og_cck_field_gdrive_client_id' );
+	$droogle_og_clientsecret_cck_field = variable_get ( 'droogle_og_cck_field_gdrive_client_secret' );
+	$droogle_default_domain = variable_get ( 'droogle_default_domain', '' );
+	
+	$form ['droogle_default_domain'] = array (
+			'#type' => 'textfield',
+			'#title' => t ( 'Enter the default domain to use to form links to your google docs ie: babson.edu, if your email address is simply yourname@gmail.com then do not worry about this, but if you are using a google education or business account and your gmail address is yourname@babson.edu for instance then enter a value for this field' ),
+			'#default_value' => $droogle_default_domain,
+			'#size' => 60,
+			'#maxlength' => 64,
+			'#description' => t ( 'the default domain to use in creating links to your google docs' )
+	);
+	$form ['sitewide_nonog'] = array (
+			'#type' => 'fieldset',
+			'#title' => t ( 'Settings controlling the non Organic Groups context for Droogle' ),
+			'#collapsible' => TRUE,
+			'#collapsed' => TRUE
+	);
+	$form ['sitewide_nonog'] ['droogle_title_text'] = array (
+			'#type' => 'textfield',
+			'#title' => t ( 'Enter the title to put at the top of the droogle page (when not within an Organic Groups context)' ),
+			'#default_value' => $droogle_title_text,
+			'#size' => 60,
+			'#maxlength' => 64,
+			'#description' => t ( 'the non OG title of the droogle page, default is:  "DROOGLE: A list of your google docs"' )
+	);
+	$form ['sitewide_nonog'] ['droogle_clientid'] = array (
+			'#type' => 'textfield',
+			'#title' => t ( 'Enter the google client id to use (when not within an Organic Groups context)
+                  -- you can generate a client id at https://cloud.google.com/console' ),
+			'#default_value' => $droogle_clientid,
+			'#size' => 100,
+			'#maxlength' => 150,
+			'#description' => t ( 'the non OG google client id' )
+	);
+	$form ['sitewide_nonog'] ['droogle_secret'] = array (
+			'#type' => 'textfield',
+			'#title' => t ( 'Enter the google client secret to use (when not within an Organic Groups context)
+                  -- you can generate a client secret at https://cloud.google.com/console' ),
+			'#default_value' => $droogle_secret,
+			'#size' => 60,
+			'#maxlength' => 64,
+			'#description' => t ( 'the non OG google client secret' )
+	);
+	$form ['sitewide_nonog'] ['droogle_refresh_token'] = array (
+			'#type' => 'textfield',
+			'#title' => t ( 'Enter the google refresh token, generate one by going to
+      the url http://yoururl.com/droogle_get_google_token but MAKE SURE TO ENABLE THE DEVEL MODULE FIRST,
+      you can disable the devel module after you generate the refresh token, ignore the error and
+      copy and paste the refesh token generated (not the access code) into this text box' ),
+			'#default_value' => $droogle_refresh_token,
+			'#size' => 60,
+			'#maxlength' => 64,
+			'#description' => t ( 'the google refresh token so the login to google does not timeout' )
+	);
+	if (module_exists ( 'og' )) {
+		$form ['single_groups'] = array (
+				'#type' => 'fieldset',
+				'#title' => t ( 'Overriding google username and password for individual Organic Group(s)' ),
+				'#collapsible' => TRUE,
+				'#collapsed' => TRUE
+		);
+		$form ['single_groups'] ['droogle_og_cck_field_gdrive_client_id'] = array (
+				'#type' => 'textfield',
+				'#title' => t ( 'Enter the "group" content types cck field machine name to use to override
+                    a particular groups google client id)' ),
+				'#default_value' => $droogle_og_clientid_cck_field,
+				'#size' => 60,
+				'#maxlength' => 64,
+				'#description' => t ( 'the OG group cck field to use to set a particular OG droogle password
+                          -- you will have to setup a new cck field for the "group" content type
+                          matching the machine name set on this page' )
+		);
+		$form ['single_groups'] ['droogle_og_cck_field_gdrive_client_secret'] = array (
+				'#type' => 'textfield',
+				'#title' => t ( 'Enter the "group" content types cck field machine name to use to override
+                    a particular groups google client secret -- this is optional and if the field
+                    is left empty when configuring a particular group, the global Droogle organic
+                    groups settings will be followed)' ),
+				'#default_value' => $droogle_og_clientsecret_cck_field,
+				'#size' => 60,
+				'#maxlength' => 64,
+				'#description' => t ( 'the OG group cck field to use to set a particular OG droogle password
+                          -- you will have to setup a new cck field for the "group" content type
+                          matching the machine name set on this page' )
+		);
+	}
+	$form = system_settings_form ( $form );
+	$form ['#submit'] [] = 'droogle_settings_form_submit';
+	return $form;
+}
+
+/**
+ * Form submission handler droogle_settings_form().
+ */
+function droogle_settings_form_submit($form, &$form_state) {
+}
+function droogle_get_google_token() {
+	$url = "https://accounts.google.com/o/oauth2/auth";
+	$client_id = variable_get ( 'droogle_clientid', '' );
+	$client_secret = variable_get ( 'droogle_secret', '' );
+	$redirect_uri = "http://" . $_SERVER ['HTTP_HOST'] . "/droogle_get_google_token";
+	$access_type = "offline";
+	$approval_prompt = "auto";
+	$grant_type = "authorization_code";
+	$scope = "https://www.googleapis.com/auth/drive";
+	
+	$params_request = array (
+			"response_type" => "code",
+			"client_id" => "$client_id",
+			"redirect_uri" => "$redirect_uri",
+			"access_type" => "$access_type",
+			"approval_prompt" => "$approval_prompt",
+			"scope" => "$scope"
+	);
+	
+	$request_to = $url . '?' . http_build_query ( $params_request );
+	
+	if (isset ( $_GET ['code'] )) {
+		// try to get an access token
+		$code = $_GET ['code'];
+		$url = 'https://accounts.google.com/o/oauth2/token';
+		$params = array (
+				"code" => $code,
+				"client_id" => "$client_id",
+				"client_secret" => "$client_secret",
+				"redirect_uri" => "$redirect_uri",
+				"grant_type" => "$grant_type"
+		);
+		
+		$curl = curl_init ( $url );
+		curl_setopt ( $curl, CURLOPT_POST, true );
+		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $params );
+		curl_setopt ( $curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
+		curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
+		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
+		
+		$json_response = curl_exec ( $curl );
+		curl_close ( $curl );
+		
+		$authObj = json_decode ( $json_response );
+		$result = array ();
+		$result ['refresh_token'] = $authObj->refresh_token;
+		$result ['access_token'] = $authObj->access_token;
+	} else {
+		header ( "Location: " . $request_to );
+	}
+	dpm ( $result );
+	return 'refesh token is ' . $result ['refresh_token'];
+}
+
+/**
+ * Implements hook_block_info().
+ */
+function droogle_block_info() {
+	$blocks = array ();
+	$blocks ['droogle-og-block'] = array (
+			'info' => t ( 'Droogle OG Block' )
+	);
+	return $blocks;
+}
+
+/**
+ * Implements hook_block_view().
+ */
+function droogle_block_view($delta = '') {
+	$block = array ();
+	switch ($delta) {
+		case 'droogle-og-block' :
+			$block ['subject'] = t ( 'Droogle OG Block' );
+			$block ['content'] = droogle_list_files ();
+			break;
+	}
+	return $block;
+}
+
+/*
+ * Function connects to google drive.
+ */
+function droogle_gdrive_connect($redirect_uri = NULL, $client_id = NULL, $client_secret = NULL, $redirect_uri = NULL) {
+	// watchdog ( 'redirect_uri', 'redirect is ' . $redirect_uri );
+	if ($path = libraries_get_path ( 'google-api-php-client-git-version' )) {
+		// ini_set ( 'include_path', ini_get ( 'include_path' ) . ':' . $_SERVER ['DOCUMENT_ROOT'] . '/sites/all/libraries/google-api-php-client-git-version/src' );
+		require_once $path . '/src/Google/autoload.php';
+		require_once $path . '/src/Google/Client.php';
+		require_once $path . '/src/Google/Http/MediaFileUpload.php';
+		require_once $path . '/src/Google/Service/Drive.php';
+	} else {
+		drupal_set_message ( 'You need to install the google api client library in the folder
+      google-api-php-client-git-version within the sites/all/libraries folder' );
+	}
+	
+	/**
+	 * **********************************************
+	 * ATTENTION: Fill in these values! Make sure
+	 * the redirect URI is to this page, e.g:
+	 * http://localhost:8080/fileupload.php
+	 * **********************************************
+	 */
+	if (module_exists ( 'og_context' )) {
+		$group_google_clientid_cck = 'field_' . variable_get ( 'droogle_og_cck_field_gdrive_client_id' );
+		$group_google_clientsecret_cck = 'field_' . variable_get ( 'droogle_og_cck_field_gdrive_client_secret' );
+		$current_group = og_context ();
+		if (isset ( $current_group->etid )) {
+			$group_node = node_load ( $current_group->etid );
+			$client_id = $group_node->{$group_google_clientid_cck} ['und'] [0] ['value'];
+			$client_secret = $group_node->{$group_google_clientsecret_cck} ['und'] [0] ['value'];
+		}
+	}
+	
+	if (! isset ( $droogle_clientid ) || ! isset ( $droogle_secret )) {
+		$droogle_clientid = variable_get ( 'droogle_clientid', '' );
+		$droogle_secret = variable_get ( 'droogle_secret', '' );
+	}
+	$client = new Google_Client ();
+	$client->setClientId ( $droogle_clientid );
+	$client->setClientSecret ( $droogle_secret );
+	$client->setRedirectUri ( 'http://' . $_SERVER ['HTTP_HOST'] . '/droogle' . $redirect_uri );
+	$client->addScope ( "https://www.googleapis.com/auth/drive" );
+	$service = new Google_Service_Drive ( $client );
+	
+	if (isset ( $_REQUEST ['logout'] )) {
+		unset ( $_SESSION ['upload_token '] );
+	}
+	
+	if (isset ( $_GET ['code'] )) {
+		$client->authenticate ( $_GET ['code'] );
+		$_SESSION ['upload_token'] = $client->getAccessToken ();
+		$redirect = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'];
+		header ( 'Location: ' . filter_var ( $redirect, FILTER_SANITIZE_URL ) );
+	}
+	
+	if (isset ( $_SESSION ['upload_token'] ) && $_SESSION ['upload_token']) {
+		$client->setAccessToken ( $_SESSION ['upload_token'] );
+		if ($client->isAccessTokenExpired ()) {
+			unset ( $_SESSION ['upload_token'] );
+		}
+	} else {
+		$authUrl = $client->createAuthUrl ();
+	}
+	$return_result ['client'] = $client;
+	$return_result ['service'] = $service;
+	if (isset ( $authUrl )) {
+		$return_result ['authUrl'] = $authUrl;
+	}
+	return $return_result;
+}
+
+/*
+ * Function creates a google drive document.
+ * This functionality works but the module will
+ * do more with it soon in coming versions.
+ */
+function droogle_file_upload($doctype = 'word', $doc_title = NULL, $role = NULL, $type = NULL, $permission_values = NULL) {
+	unset ( $result );
+	$return_result = droogle_gdrive_connect ( 'droogle_gdrive_create_file' );
+	$client = $return_result ['client'];
+	$service = $return_result ['service'];
+	if (isset ( $return_result ['authUrl'] )) {
+		$authUrl = $return_result ['authUrl'];
+	}
+	/**
+	 * **********************************************
+	 * If we're signed in then lets try to upload our
+	 * file.
+	 * **********************************************
+	 */
+	$refresh_token = variable_get ( 'droogle_refresh_token', '' );
+	if (! empty ( $refresh_token ) && $client->isAccessTokenExpired ()) {
+		$client->refreshToken ( $refresh_token );
+	}
+	
+	if ($client->getAccessToken ()) {
+		$file = new Google_Service_Drive_DriveFile ();
+		if (! isset ( $doc_title )) {
+			$doc_title = 'Hello world from the new api! ' . time ();
+		}
+		$file->setTitle ( $doc_title );
+		switch ($doctype) {
+			case 'word' :
+				$file->setMimeType ( 'application/vnd.google-apps.document' );
+				break;
+			case 'excel' :
+				$file->setMimeType ( 'application/vnd.google-apps.spreadsheet' );
+				break;
+			case 'presentation' :
+				$file->setMimeType ( 'application/vnd.google-apps.presentation' );
+				break;
+			case 'drawing' :
+				$file->setMimeType ( 'application/vnd.google-apps.drawing' );
+				break;
+		}
+		
+		$result = $service->files->insert ( $file );
+		// Permission documentation is here: https://developers.google.com/drive/v2/reference/permissions and
+		// here https://developers.google.com/drive/web/manage-sharing
+		$permission = new Google_Service_Drive_Permission ();
+		if (isset ( $permission_values )) {
+			foreach ( $permission_values as $value ) {
+				$permission->setValue ( $value );
+			}
+		} else {
+			$permission->setValue ( variable_get ( 'site_mail', 'wcheung@babson.edu' ) );
+		}
+		
+		if (isset ( $type )) {
+			$permission->setType ( $type );
+		} else {
+			$permission->setType ( 'user' );
+		}
+		
+		if (isset ( $role )) {
+			$permission->setRole ( $role );
+		} else {
+			$permission->setRole ( 'writer' );
+		}
+		
+		$service->permissions->insert ( $result ['id'], $permission );
+	}
+	
+	$content = '';
+	if (isset ( $authUrl )) {
+		$content .= '<a class="login" href=' . $authUrl . '>Connect Me!</a></div>';
+	}
+	if (isset ( $result ) && $result) {
+		// $content .= '<div class="shortened"><pre> ' . print_r($result, TRUE) . '</pre></div></div>';
+		drupal_set_message ( 'File was Created in Google Drive!' );
+	}
+	return $content;
+}
+
+/*
+ * A page callback for when you go to the droogle url.
+ */
+function droogle_list_files() {
+	$droogle_title_text = variable_get ( 'droogle_title_text', '' );
+	drupal_set_title ( $droogle_title_text );
+	unset ( $result );
+	$return_result = droogle_gdrive_connect ( 'droogle' );
+	$client = $return_result ['client'];
+	$service = $return_result ['service'];
+	if (isset ( $return_result ['authUrl'] )) {
+		$authUrl = $return_result ['authUrl'];
+	}
+	
+	/**
+	 * **********************************************
+	 * If we're signed in then lets try to upload our
+	 * file.
+	 * **********************************************
+	 */
+	$refresh_token = variable_get ( 'droogle_refresh_token', '' );
+	
+	if (! empty ( $refresh_token ) && $client->isAccessTokenExpired ()) {
+		$client->refreshToken ( $refresh_token );
+	}
+	if ($client->getAccessToken ()) {
+		$content = theme ( 'droogle_list_files', array (
+				'service' => $service
+		) );
+	} elseif (isset ( $authUrl )) {
+		$content = '<a class="login" href=' . $authUrl . '>Connect Me!</a></div>';
+	}
+	return $content;
+}
+
+/**
+ * Droogle file upload form.
+ */
+function droogle_upload_form($form, &$form_state) {
+	drupal_set_title ( 'Upload file to google drive' );
+	$form = array ();
+	
+	$form ['file'] = array (
+			'#type' => 'file',
+			'#title' => t ( 'Upload file' ),
+			'#description' => t ( 'Allowed types are pdf txt doc docx xls xlsx ppt pptx jpg jpeg png' )
+	);
+	
+	$form ['#validate'] = array (
+			'droogle_upload_validate'
+	);
+	
+	$form ['submit'] = array (
+			'#type' => 'submit',
+			'#value' => 'Submit'
+	);
+	
+	return $form;
+}
+
+/**
+ * Validate handler for droogle file upload form.
+ */
+function droogle_upload_validate(&$form, &$form_state) {
+	$validators = array (
+			'file_validate_extensions' => array (
+					'pdf txt doc docx xls xlsx ppt pptx jpg jpeg png'
+			)
+	);
+	
+	$destination = "public://drive_uploads";
+	file_prepare_directory ( $destination, FILE_CREATE_DIRECTORY );
+	
+	if ($file = file_save_upload ( 'file', $validators, $destination, FILE_EXISTS_RENAME )) {
+		$form_state ['values'] ['file'] = $file;
+	} else {
+		form_set_error ( 'file', t ( 'Unable to copy upload file to !dest', array (
+				'!dest' => $destination
+		) ) );
+	}
+}
+
+/**
+ * Submit handler for droogle file upload form.
+ */
+function droogle_upload_form_submit($form, &$form_state) {
+	$file = $form_state ['values'] ['file'];
+	if (isset ( $file->uri )) {
+		$status = _droogle_upload_to_drive ( $file );
+		if ($status == TRUE) {
+			file_delete ( $form_state ['values'] ['file'] );
+		}
+	}
+}
+
+/**
+ * Upload file to google drive
+ *
+ * @param Class $upload_file
+ * @return string
+ */
+function _droogle_upload_to_drive($upload_file) {
+	unset ( $result );
+	$return_result = droogle_gdrive_connect ( 'droogle' );
+	$client = $return_result ['client'];
+	$service = $return_result ['service'];
+	if (isset ( $return_result ['authUrl'] )) {
+		$authUrl = $return_result ['authUrl'];
+	}
+	/**
+	 * **********************************************
+	 * If we're signed in then lets try to upload our
+	 * file.
+	 * **********************************************
+	 */
+	$refresh_token = variable_get ( 'droogle_refresh_token', '' );
+	if (! empty ( $refresh_token ) && $client->isAccessTokenExpired ()) {
+		$client->refreshToken ( $refresh_token );
+	}
+	
+	if ($client->getAccessToken ()) {
+		$file = new Google_Service_Drive_DriveFile ();
+		
+		$filename = basename ( $upload_file->filename );
+		if (! isset ( $filename )) {
+			$filename = 'New file ' . time ();
+		}
+		$file->setTitle ( $filename );
+		
+		$uploadType = 'multipart';
+		$mimeType = $upload_file->filemime;
+		$data = $data = file_get_contents ( file_create_url ( $upload_file->uri ) );
+		$result = $service->files->insert ( $file, array (
+				'data' => $data,
+				'mimeType' => $mimeType,
+				'uploadType' => $uploadType,
+				'fields' => 'id'
+		) );
+		
+		// Permission documentation is here: https://developers.google.com/drive/v2/reference/permissions and
+		// here https://developers.google.com/drive/web/manage-sharing
+		$permission = new Google_Service_Drive_Permission ();
+		if (isset ( $permission_values )) {
+			foreach ( $permission_values as $value ) {
+				$permission->setValue ( $value );
+			}
+		} else {
+			$permission->setValue ( variable_get ( 'site_mail', 'wcheung@babson.edu' ) );
+		}
+		
+		if (isset ( $type )) {
+			$permission->setType ( $type );
+		} else {
+			$permission->setType ( 'user' );
+		}
+		
+		if (isset ( $role )) {
+			$permission->setRole ( $role );
+		} else {
+			$permission->setRole ( 'writer' );
+		}
+		
+		$service->permissions->insert ( $result ['id'], $permission );
+	}
+	
+	$content = '';
+	if (isset ( $authUrl )) {
+		$content .= '<a class="login" href=' . $authUrl . '>Connect Me!</a></div>';
+	}
+	if (isset ( $result ) && $result) {
+		drupal_set_message ( 'File was Created in Google Drive!' );
+	}
+	return $content;
+}
+
+/**
+ * Implements hook_theme().
+ *
+ * Information on hook theme http://groups.drupal.org/node/14274#comment-57950.
+ * 1. hook_theme which adds an array of callbacks and their arguments to the
+ * theme registry. I didn't realize that I had to visit the modules page to
+ * rebuild the theme registry before it would be added.
+ * 2. The themable function itself which starts with theme_ followed by the
+ * function name that was added to the registry with hook_theme.
+ * 3. theme("whatever_function, $whatever_argument, $whatever_argument ")
+ * which actually calls the function.
+ */
+function droogle_theme() {
+	return array (
+			'droogle_list_files' => array (
+					'arguments' => array (
+							'service' => NULL
+					)
+			)
+	);
+}
+
+/**
+ * Implements of hook_theme().
+ *
+ * @param array $variables
+ *        	an array with google document listings.
+ *
+ * @return string html with listing of google docs to be output to the screen.
+ */
+function theme_droogle_list_files($variables) {
+	drupal_add_css ( drupal_get_path ( 'module', 'droogle' ) . '/css/droogle.css' );
+	$service = $variables ['service'];
+	$files = $service->files->listFiles ();
+	$file_list = $files->getItems ();
+	$content = '<ul>';
+	// Below line if for debugging.
+	// $content .= '<div class="shortened">File List: <pre> ' . print_r($file_list, TRUE) . '</pre></div></div>';
+	foreach ( $file_list as $file ) {
+		if ($file ['mimeType'] == 'application/vnd.google-apps.folder' || (isset ( $file ['explicitlyTrashed'] ) && $file ['explicitlyTrashed'] == 1)) {
+			// Don't list folders, just files (for now). It's displaying all files in folders anyhow.
+			continue;
+		}
+		switch ($file ['mimeType']) {
+			case 'application/vnd.google-apps.document' :
+			case 'application/msword' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="word">' . $file ["title"] . '</li></a>';
+				break;
+			case 'application/vnd.google-apps.spreadsheet' :
+			case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="excel">' . $file ["title"] . '</li></a>';
+				break;
+			case 'application/vnd.google-apps.presentation' :
+			case 'application/vnd.openxmlformats-officedocument.presentationml.presentation' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="presentation">' . $file ["title"] . '</li></a>';
+				break;
+			case 'application/vnd.google-apps.drawing' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="drawing">' . $file ["title"] . '</li></a>';
+				break;
+			case 'application/pdf' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="pdf">' . $file ["title"] . '</li></a>';
+				break;
+			case 'image/png' :
+			case 'image/jpg' :
+			case 'image/jpeg' :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="drawing">' . $file ["title"] . '</li></a>';
+				break;
+			default :
+				$content .= '<a href="' . $file ["alternateLink"] . '" target="_blank"><li class="word excel presentation pdf drawing">' . $file ["title"] . '</li></a>';
+		}
+	}
+	$content .= '</ul>';
+	
+	if (isset ( $result ) && $result) {
+		// For debugging
+		// $content .= '<div class="shortened"><pre> ' . print_r($result, TRUE) . '</pre></div></div>';
+	}
+	return $content;
+}
+
+
