00001 # TKE - Advanced Programmer's Editor 00002 # Copyright (C) 2014-2019 Trevor Williams (phase1geo@gmail.com) 00003 # 00004 # This program is free software; you can redistribute it and/or modify 00005 # it under the terms of the GNU General Public License as published by 00006 # the Free Software Foundation; either version 2 of the License, or 00007 # (at your option) any later version. 00008 # 00009 # This program is distributed in the hope that it will be useful, 00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 # GNU General Public License for more details. 00013 # 00014 # You should have received a copy of the GNU General Public License along 00015 # with this program; if not, write to the Free Software Foundation, Inc., 00016 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 00018 ###################################################################### 00019 # Name: favorites.tcl 00020 # Author: Trevor Williams (trevorw@sgi.com) 00021 # Date: 07/17/2014 00022 # Brief: Handles functionality associated with favorite files/directories. 00023 ###################################################################### 00024 00025 namespace eval favorites { 00026 00027 variable favorites_file [file join $::tke_home favorites.dat] 00028 variable items 00029 00030 ###################################################################### 00031 # Loads the favorite information file into memory. 00032 proc load {} { 00033 00034 variable favorites_file 00035 variable items 00036 00037 set items [list] 00038 00039 if {![catch { open $favorites_file r } rc]} { 00040 set items [::read $rc] 00041 close $rc 00042 } 00043 00044 # Add a normalized 00045 for {set i 0} {$i < [llength $items]} {incr i} { 00046 lset items $i 2 [files::normalize {*}[lrange [lindex $items $i] 0 1]] 00047 } 00048 00049 } 00050 00051 ###################################################################### 00052 # Stores the favorite information back out to the file. 00053 proc store {} { 00054 00055 variable favorites_file 00056 variable items 00057 00058 if {![catch { open $favorites_file w } rc]} { 00059 foreach item $items { 00060 puts $rc [list [list {*}[lrange $item 0 1] ""]] 00061 } 00062 close $rc 00063 } 00064 00065 } 00066 00067 ###################################################################### 00068 # Adds a file to the list of favorites. 00069 proc add {fname} { 00070 00071 variable items 00072 00073 # Only add the file if it currently does not exist 00074 if {[lsearch -index 2 $items $fname] == -1} { 00075 lappend items [list [info hostname] $fname [files::normalize [info hostname] $fname]] 00076 store 00077 return 1 00078 } 00079 00080 return 0 00081 00082 } 00083 00084 ###################################################################### 00085 # Removes the given filename from the list of favorites. 00086 proc remove {fname} { 00087 00088 variable items 00089 00090 # Only remove the file if it currently exists in the list 00091 if {[set index [lsearch -index 2 $items $fname]] != -1} { 00092 set items [lreplace $items $index $index] 00093 store 00094 return 1 00095 } 00096 00097 return 0 00098 00099 } 00100 00101 ###################################################################### 00102 # Returns the normalized filenames based on the current host. 00103 proc get_list {} { 00104 00105 variable items 00106 00107 set item_list [list] 00108 00109 foreach item $items { 00110 if {[file exists [lindex $item 2]]} { 00111 lappend item_list [lindex $item 2] 00112 } 00113 } 00114 00115 return [lsort $item_list] 00116 00117 } 00118 00119 ###################################################################### 00120 # Returns 1 if the given filename is marked as a favorite. 00121 proc is_favorite {fname} { 00122 00123 variable items 00124 00125 return [expr [lsearch -index 2 $items $fname] != -1] 00126 00127 } 00128 00129 ###################################################################### 00130 # Displays the launcher with favorited files/directories. 00131 proc launcher {} { 00132 00133 # Add favorites to launcher 00134 foreach item [get_list] { 00135 if {[file isdirectory $item]} { 00136 launcher::register_temp "`FAVORITE:$item" "sidebar::add_directory $item" $item 00137 } else { 00138 launcher::register_temp "`FAVORITE:$item" "gui::add_file end $item" $item 00139 } 00140 } 00141 00142 # Display the launcher in FAVORITE: mode 00143 launcher::launch "`FAVORITE:" 00144 00145 # Unregister the favorites 00146 foreach item [get_list] { 00147 launcher::unregister "`FAVORITE:$item" 00148 } 00149 00150 } 00151 00152 ###################################################################### 00153 # Returns a list of files/directories associated with the favorites for 00154 # the purposes of settings sharing. 00155 proc get_share_items {dir} { 00156 00157 return [list favorites.dat] 00158 00159 } 00160 00161 ###################################################################### 00162 # Called when the share directory changes. 00163 proc share_changed {dir} { 00164 00165 variable favorites_file 00166 00167 set favorites_file [file join $dir favorites.dat] 00168 00169 } 00170 00171 } 00172