2017-04-15 06:32:05 -04:00
|
|
|
/*
|
2014-05-13 04:45:59 -04:00
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2020-01-01 06:23:12 -05:00
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2014-05-13 04:45:59 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2017-04-15 06:32:05 -04:00
|
|
|
|
2014-08-22 03:26:46 -04:00
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
2014-05-13 04:45:59 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
2015-07-31 04:21:32 -04:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
2014-05-13 04:45:59 -04:00
|
|
|
#include "rofi.h"
|
|
|
|
#include "history.h"
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2014-05-13 04:45:59 -04:00
|
|
|
|
2016-10-14 12:56:09 -04:00
|
|
|
/**
|
|
|
|
* History element
|
|
|
|
*/
|
2014-05-13 04:45:59 -04:00
|
|
|
typedef struct __element
|
|
|
|
{
|
2016-10-14 12:56:09 -04:00
|
|
|
/** Index in history */
|
2014-05-13 04:45:59 -04:00
|
|
|
long int index;
|
2016-10-14 12:56:09 -04:00
|
|
|
/** Entry */
|
2016-04-10 07:55:18 -04:00
|
|
|
char *name;
|
2014-05-13 04:45:59 -04:00
|
|
|
}_element;
|
|
|
|
|
2015-07-05 03:47:55 -04:00
|
|
|
static int __element_sort_func ( const void *ea, const void *eb, void *data __attribute__( ( unused ) ) )
|
2014-05-13 04:45:59 -04:00
|
|
|
{
|
|
|
|
_element *a = *(_element * *) ea;
|
|
|
|
_element *b = *(_element * *) eb;
|
|
|
|
return b->index - a->index;
|
|
|
|
}
|
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
|
2014-05-13 04:45:59 -04:00
|
|
|
{
|
2014-08-29 06:14:43 -04:00
|
|
|
if ( list == NULL || length == 0 ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Sort the list before writing out.
|
2015-07-05 03:47:55 -04:00
|
|
|
g_qsort_with_data ( list, length, sizeof ( _element* ), __element_sort_func, NULL );
|
2014-05-13 04:45:59 -04:00
|
|
|
|
2014-05-25 11:55:27 -04:00
|
|
|
// Get minimum index.
|
2014-05-25 17:32:06 -04:00
|
|
|
int min_value = list[length - 1]->index;
|
2014-05-25 11:55:27 -04:00
|
|
|
|
2014-05-13 04:45:59 -04:00
|
|
|
// Set the max length of the list.
|
2017-10-05 11:45:50 -04:00
|
|
|
length = ( length > config.max_history_size ) ? config.max_history_size : length;
|
2014-05-13 04:45:59 -04:00
|
|
|
|
|
|
|
// Write out entries.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2014-05-25 17:32:06 -04:00
|
|
|
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
|
2014-05-22 03:33:32 -04:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-14 07:17:03 -04:00
|
|
|
static char ** __history_get_element_list_fields ( FILE *fd, unsigned int *length )
|
|
|
|
{
|
|
|
|
unsigned int real_length = 0;
|
2017-11-07 14:04:07 -05:00
|
|
|
char **retv = NULL;;
|
2017-10-14 07:17:03 -04:00
|
|
|
if ( length == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*length = 0;
|
|
|
|
|
|
|
|
if ( fd == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
ssize_t l = 0;
|
|
|
|
while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
|
|
|
|
// Jump to the first space.
|
2017-11-07 14:04:07 -05:00
|
|
|
const char *start = strchr ( buffer, ' ' );
|
2017-10-14 07:17:03 -04:00
|
|
|
// not found, skip.
|
2017-11-07 14:04:07 -05:00
|
|
|
if ( start == NULL ) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-14 07:17:03 -04:00
|
|
|
start++;
|
|
|
|
// remove trailing \n
|
|
|
|
buffer[l - 1] = '\0';
|
|
|
|
if ( real_length < ( *length + 2 ) ) {
|
|
|
|
real_length += 15;
|
|
|
|
// Resize and check.
|
|
|
|
retv = g_realloc ( retv, ( real_length ) * sizeof ( char* ) );
|
|
|
|
}
|
|
|
|
// Parse the number of times.
|
|
|
|
retv[( *length )] = g_strndup ( start, l - 1 - ( start - buffer ) );
|
|
|
|
// Force trailing '\0'
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
2018-08-08 09:55:13 -04:00
|
|
|
if ( buffer_length > 0 ) {
|
2018-05-16 14:41:29 -04:00
|
|
|
g_free ( buffer );
|
|
|
|
}
|
2017-10-14 07:17:03 -04:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2014-05-13 04:45:59 -04:00
|
|
|
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
|
|
|
|
{
|
2017-09-29 02:32:09 -04:00
|
|
|
unsigned int real_length = 0;
|
2017-10-05 11:45:50 -04:00
|
|
|
_element **retv = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( length == NULL ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*length = 0;
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( fd == NULL ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-10 08:30:13 -04:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
ssize_t l = 0;
|
|
|
|
while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
|
2014-05-17 17:06:45 -04:00
|
|
|
char * start = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
// Skip empty lines.
|
2016-04-27 06:55:48 -04:00
|
|
|
if ( l <= 1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
long int index = strtol ( buffer, &start, 10 );
|
|
|
|
if ( start == buffer || *start == '\0' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
start++;
|
2016-05-27 03:31:57 -04:00
|
|
|
if ( ( l - ( start - buffer ) ) < 2 ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-05 11:45:50 -04:00
|
|
|
if ( real_length < ( *length + 2 ) ) {
|
2017-09-29 02:32:09 -04:00
|
|
|
real_length += 15;
|
|
|
|
// Resize and check.
|
|
|
|
retv = g_realloc ( retv, ( real_length ) * sizeof ( _element* ) );
|
|
|
|
}
|
2014-05-26 03:19:58 -04:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
retv[( *length )] = g_malloc ( sizeof ( _element ) );
|
2016-04-27 06:55:48 -04:00
|
|
|
|
2014-05-13 04:45:59 -04:00
|
|
|
// remove trailing \n
|
2016-04-27 06:55:48 -04:00
|
|
|
buffer[l - 1] = '\0';
|
2014-05-13 04:45:59 -04:00
|
|
|
// Parse the number of times.
|
2016-04-27 06:55:48 -04:00
|
|
|
retv[( *length )]->index = index;
|
|
|
|
retv[( *length )]->name = g_strndup ( start, l - 1 - ( start - buffer ) );
|
2014-05-13 04:45:59 -04:00
|
|
|
// Force trailing '\0'
|
2016-04-10 08:30:13 -04:00
|
|
|
retv[( *length ) + 1] = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
( *length )++;
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
2016-04-10 07:55:18 -04:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void history_set ( const char *filename, const char *entry )
|
|
|
|
{
|
2014-06-05 15:55:47 -04:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-30 18:35:00 -05:00
|
|
|
|
|
|
|
// Check if program should be ignored
|
|
|
|
for ( char *checked_prefix = strtok ( config.ignored_prefixes, ";" ); checked_prefix != NULL; checked_prefix = strtok ( NULL, ";" ) ) {
|
|
|
|
// For each ignored prefix
|
|
|
|
|
|
|
|
while ( g_unichar_isspace ( g_utf8_get_char ( checked_prefix ) ) ) {
|
|
|
|
checked_prefix = g_utf8_next_char ( checked_prefix ); // Some users will probably want "; " as their separator for aesthetics.
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( g_str_has_prefix ( entry, checked_prefix ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
int found = 0;
|
2014-05-13 04:45:59 -04:00
|
|
|
unsigned int curr = 0;
|
|
|
|
unsigned int length = 0;
|
2014-05-22 03:33:32 -04:00
|
|
|
_element **list = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
// Open file for reading and writing.
|
2016-01-03 14:13:08 -05:00
|
|
|
FILE *fd = g_fopen ( filename, "r" );
|
|
|
|
if ( fd != NULL ) {
|
|
|
|
// Get list.
|
|
|
|
list = __history_get_element_list ( fd, &length );
|
|
|
|
// Close file, if fails let user know on stderr.
|
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
|
2016-01-03 14:13:08 -05:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
// Look if the entry exists.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
|
|
|
|
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
curr = iter;
|
2014-05-13 04:45:59 -04:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( found ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
// If exists, increment list index number
|
|
|
|
list[curr]->index++;
|
2014-05-22 03:33:32 -04:00
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else{
|
2014-05-13 04:45:59 -04:00
|
|
|
// If not exists, add it.
|
|
|
|
// Increase list by one
|
2014-08-09 05:40:42 -04:00
|
|
|
list = g_realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
|
|
|
|
list[length] = g_malloc ( sizeof ( _element ) );
|
|
|
|
// Copy name
|
|
|
|
if ( list[length] != NULL ) {
|
2016-04-10 08:30:13 -04:00
|
|
|
list[length]->name = g_strdup ( entry );
|
2014-08-09 05:40:42 -04:00
|
|
|
// set # hits
|
|
|
|
list[length]->index = 1;
|
2014-05-26 03:19:58 -04:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
length++;
|
|
|
|
list[length] = NULL;
|
2014-05-26 03:19:58 -04:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
|
2016-01-03 14:13:08 -05:00
|
|
|
fd = fopen ( filename, "w" );
|
|
|
|
if ( fd == NULL ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
|
2016-01-03 14:13:08 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-05-13 16:11:42 -04:00
|
|
|
// Write list.
|
2014-05-22 03:33:32 -04:00
|
|
|
__history_write_element_list ( fd, list, length );
|
2016-01-03 14:13:08 -05:00
|
|
|
// Close file, if fails let user know on stderr.
|
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
|
2016-01-03 14:13:08 -05:00
|
|
|
}
|
2014-05-13 16:11:42 -04:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
// Free the list.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2016-04-10 07:55:18 -04:00
|
|
|
g_free ( list[iter]->name );
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( list[iter] );
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( list );
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void history_remove ( const char *filename, const char *entry )
|
|
|
|
{
|
2014-06-05 15:55:47 -04:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
_element ** list = NULL;
|
|
|
|
int found = 0;
|
|
|
|
unsigned int curr = 0;
|
|
|
|
unsigned int length = 0;
|
2014-05-13 04:45:59 -04:00
|
|
|
// Open file for reading and writing.
|
2016-01-03 14:13:08 -05:00
|
|
|
FILE *fd = g_fopen ( filename, "r" );
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( fd == NULL ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
|
2014-05-22 03:33:32 -04:00
|
|
|
return;
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
// Get list.
|
2014-05-22 03:33:32 -04:00
|
|
|
list = __history_get_element_list ( fd, &length );
|
|
|
|
|
2016-01-03 14:13:08 -05:00
|
|
|
// Close file, if fails let user know on stderr.
|
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
|
2016-01-03 14:13:08 -05:00
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
// Find entry.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
|
|
|
|
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
curr = iter;
|
2014-05-13 04:45:59 -04:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If found, remove it and write out new file.
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( found ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
// Remove the entry.
|
2016-04-10 07:55:18 -04:00
|
|
|
g_free ( list[curr]->name );
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( list[curr] );
|
2014-05-13 04:45:59 -04:00
|
|
|
// Swap last to here (if list is size 1, we just swap empty sets).
|
2014-05-22 03:33:32 -04:00
|
|
|
list[curr] = list[length - 1];
|
2014-05-13 04:45:59 -04:00
|
|
|
// Empty last.
|
2014-05-22 03:33:32 -04:00
|
|
|
list[length - 1] = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
length--;
|
|
|
|
|
2016-01-03 14:13:08 -05:00
|
|
|
fd = g_fopen ( filename, "w" );
|
2014-05-13 04:45:59 -04:00
|
|
|
// Clear list.
|
2016-01-03 14:13:08 -05:00
|
|
|
if ( fd != NULL ) {
|
2014-05-13 16:11:42 -04:00
|
|
|
// Write list.
|
2014-05-22 03:33:32 -04:00
|
|
|
__history_write_element_list ( fd, list, length );
|
2016-01-03 14:13:08 -05:00
|
|
|
// Close file, if fails let user know on stderr.
|
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
|
2016-01-03 14:13:08 -05:00
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else{
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
|
2014-05-13 16:11:42 -04:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free the list.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2016-04-10 07:55:18 -04:00
|
|
|
g_free ( list[iter]->name );
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( list[iter] );
|
2014-05-22 03:33:32 -04:00
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( list != NULL ) {
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( list );
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char ** history_get_list ( const char *filename, unsigned int *length )
|
|
|
|
{
|
2014-08-29 06:14:43 -04:00
|
|
|
*length = 0;
|
|
|
|
|
2014-06-05 15:55:47 -04:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-07 14:04:07 -05:00
|
|
|
char **retv = NULL;
|
2014-05-13 04:45:59 -04:00
|
|
|
// Open file.
|
2017-11-07 14:04:07 -05:00
|
|
|
FILE *fd = g_fopen ( filename, "r" );
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( fd == NULL ) {
|
2014-05-27 02:42:21 -04:00
|
|
|
// File that does not exists is not an error, so ignore it.
|
|
|
|
// Everything else? panic.
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( errno != ENOENT ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
|
2014-05-13 04:45:59 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Get list.
|
2017-10-14 07:17:03 -04:00
|
|
|
retv = __history_get_element_list_fields ( fd, length );
|
2014-05-13 04:45:59 -04:00
|
|
|
|
2015-07-31 04:21:32 -04:00
|
|
|
// Close file, if fails let user know on stderr.
|
|
|
|
if ( fclose ( fd ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
|
2015-07-31 04:21:32 -04:00
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
return retv;
|
|
|
|
}
|