rofi/source/history.c

306 lines
8.9 KiB
C
Raw Normal View History

/*
* rofi
*
* MIT/X11 License
* Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
*
* 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.
*
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "rofi.h"
#include "history.h"
2016-01-07 15:01:56 +00:00
#include "settings.h"
2014-05-22 07:33:32 +00:00
#define HISTORY_MAX_ENTRIES 25
2016-10-14 16:56:09 +00:00
/**
* History element
*/
typedef struct __element
{
2016-10-14 16:56:09 +00:00
/** Index in history */
long int index;
2016-10-14 16:56:09 +00:00
/** Entry */
char *name;
}_element;
2015-07-05 07:47:55 +00:00
static int __element_sort_func ( const void *ea, const void *eb, void *data __attribute__( ( unused ) ) )
{
_element *a = *(_element * *) ea;
_element *b = *(_element * *) eb;
return b->index - a->index;
}
2014-05-22 07:33:32 +00:00
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{
if ( list == NULL || length == 0 ) {
return;
}
// Sort the list before writing out.
2015-07-05 07:47:55 +00:00
g_qsort_with_data ( list, length, sizeof ( _element* ), __element_sort_func, NULL );
2014-05-25 15:55:27 +00:00
// Get minimum index.
int min_value = list[length - 1]->index;
2014-05-25 15:55:27 +00:00
// Set the max length of the list.
2014-05-22 07:33:32 +00:00
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
// Write out entries.
2014-06-04 19:29:23 +00:00
for ( unsigned int iter = 0; iter < length; iter++ ) {
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
2014-05-22 07:33:32 +00:00
}
}
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
_element **retv = NULL;
2014-06-04 19:29:23 +00:00
if ( length == NULL ) {
return NULL;
}
*length = 0;
2014-06-04 19:29:23 +00:00
if ( fd == NULL ) {
return NULL;
}
2016-04-10 12:30:13 +00:00
char *buffer = NULL;
size_t buffer_length = 0;
ssize_t l = 0;
while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
char * start = NULL;
// Skip empty lines.
if ( l <= 1 ) {
continue;
}
long int index = strtol ( buffer, &start, 10 );
if ( start == buffer || *start == '\0' ) {
continue;
}
start++;
2016-05-27 07:31:57 +00:00
if ( ( l - ( start - buffer ) ) < 2 ) {
continue;
}
// Resize and check.
retv = g_realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[( *length )] = g_malloc ( sizeof ( _element ) );
// remove trailing \n
buffer[l - 1] = '\0';
// Parse the number of times.
retv[( *length )]->index = index;
retv[( *length )]->name = g_strndup ( start, l - 1 - ( start - buffer ) );
// Force trailing '\0'
2016-04-10 12:30:13 +00:00
retv[( *length ) + 1] = NULL;
2014-05-22 07:33:32 +00:00
( *length )++;
}
if ( buffer != NULL ) {
free ( buffer );
buffer = NULL;
}
return retv;
}
void history_set ( const char *filename, const char *entry )
{
2014-06-05 19:55:47 +00:00
if ( config.disable_history ) {
return;
}
2014-05-22 07:33:32 +00:00
int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
2014-05-22 07:33:32 +00:00
_element **list = NULL;
// Open file for reading and writing.
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 ) {
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
}
}
2014-05-22 07:33:32 +00:00
// Look if the entry exists.
2014-06-04 19:29:23 +00:00
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
2014-05-22 07:33:32 +00:00
curr = iter;
found = 1;
}
}
2014-06-04 19:29:23 +00:00
if ( found ) {
// If exists, increment list index number
list[curr]->index++;
2014-05-22 07:33:32 +00:00
}
2014-06-04 19:29:23 +00:00
else{
// If not exists, add it.
// Increase list by one
list = g_realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
list[length] = g_malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL ) {
2016-04-10 12:30:13 +00:00
list[length]->name = g_strdup ( entry );
// set # hits
list[length]->index = 1;
length++;
list[length] = NULL;
}
}
fd = fopen ( filename, "w" );
if ( fd == NULL ) {
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
}
else {
2014-05-13 20:11:42 +00:00
// Write list.
2014-05-22 07:33:32 +00:00
__history_write_element_list ( fd, list, length );
// Close file, if fails let user know on stderr.
if ( fclose ( fd ) != 0 ) {
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
}
2014-05-13 20:11:42 +00:00
}
// Free the list.
2014-06-04 19:29:23 +00:00
for ( unsigned int iter = 0; iter < length; iter++ ) {
g_free ( list[iter]->name );
g_free ( list[iter] );
}
g_free ( list );
}
void history_remove ( const char *filename, const char *entry )
{
2014-06-05 19:55:47 +00:00
if ( config.disable_history ) {
return;
}
2014-05-22 07:33:32 +00:00
_element ** list = NULL;
int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
// Open file for reading and writing.
FILE *fd = g_fopen ( filename, "r" );
2014-06-04 19:29:23 +00:00
if ( fd == NULL ) {
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
2014-05-22 07:33:32 +00:00
return;
}
// Get list.
2014-05-22 07:33:32 +00:00
list = __history_get_element_list ( fd, &length );
// Close file, if fails let user know on stderr.
if ( fclose ( fd ) != 0 ) {
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
}
2014-05-22 07:33:32 +00:00
// Find entry.
2014-06-04 19:29:23 +00:00
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
2014-05-22 07:33:32 +00:00
curr = iter;
found = 1;
}
}
// If found, remove it and write out new file.
2014-06-04 19:29:23 +00:00
if ( found ) {
// Remove the entry.
g_free ( list[curr]->name );
g_free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
2014-05-22 07:33:32 +00:00
list[curr] = list[length - 1];
// Empty last.
2014-05-22 07:33:32 +00:00
list[length - 1] = NULL;
length--;
fd = g_fopen ( filename, "w" );
// Clear list.
if ( fd != NULL ) {
2014-05-13 20:11:42 +00:00
// Write list.
2014-05-22 07:33:32 +00:00
__history_write_element_list ( fd, list, length );
// Close file, if fails let user know on stderr.
if ( fclose ( fd ) != 0 ) {
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
}
2014-05-22 07:33:32 +00:00
}
2014-06-04 19:29:23 +00:00
else{
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
2014-05-13 20:11:42 +00:00
}
}
// Free the list.
2014-06-04 19:29:23 +00:00
for ( unsigned int iter = 0; iter < length; iter++ ) {
g_free ( list[iter]->name );
g_free ( list[iter] );
2014-05-22 07:33:32 +00:00
}
2014-06-04 19:29:23 +00:00
if ( list != NULL ) {
g_free ( list );
}
}
char ** history_get_list ( const char *filename, unsigned int *length )
{
*length = 0;
2014-06-05 19:55:47 +00:00
if ( config.disable_history ) {
return NULL;
}
_element **list = NULL;
2014-05-22 07:33:32 +00:00
char **retv = NULL;
// Open file.
FILE *fd = g_fopen ( filename, "r" );
2014-06-04 19:29:23 +00:00
if ( fd == NULL ) {
2014-05-27 06:42:21 +00:00
// File that does not exists is not an error, so ignore it.
// Everything else? panic.
2014-06-04 19:29:23 +00:00
if ( errno != ENOENT ) {
g_warning ( "Failed to open file: %s", g_strerror ( errno ) );
}
return NULL;
}
// Get list.
2014-05-22 07:33:32 +00:00
list = __history_get_element_list ( fd, length );
2014-05-22 07:33:32 +00:00
// Copy list in right format.
2014-05-27 06:42:21 +00:00
// Lists are always short, so performance should not be an issue.
2014-06-04 19:29:23 +00:00
if ( ( *length ) > 0 ) {
retv = g_malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( unsigned int iter = 0; iter < ( *length ); iter++ ) {
retv[iter] = ( list[iter]->name );
g_free ( list[iter] );
}
2014-05-22 07:33:32 +00:00
retv[( *length )] = NULL;
g_free ( list );
}
// Close file, if fails let user know on stderr.
if ( fclose ( fd ) != 0 ) {
g_warning ( "Failed to close history file: %s", g_strerror ( errno ) );
}
return retv;
}