Skip to content

Commit a89c95c

Browse files
JombolioJombo
andauthored
Sort by File Extension for Grid and List View (#3666)
* Add support for sorting files by extension - Added NEMO_FILE_SORT_BY_EXTENSION to sort types. - Implemented compare_by_extension logic with case-insensitive grouping. - Added Extension column to List View for sorting. - Added Sort by Extension to Icon View context menu. - Added Extension sort option to Desktop View and Customization Overlay. Co-authored-by: Jombo <[email protected]>
1 parent 46012b9 commit a89c95c

8 files changed

Lines changed: 122 additions & 18 deletions

File tree

gresources/nemo-desktop-overlay.glade

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
<col id="1" translatable="yes">Date</col>
132132
<col id="2">Desktop Sort by Date</col>
133133
</row>
134+
<row>
135+
<col id="0">10</col>
136+
<col id="1" translatable="yes">Extension</col>
137+
<col id="2">Desktop Sort by Extension</col>
138+
</row>
134139
</data>
135140
</object>
136141
<object class="XAppGtkWindow" id="overlay_window">

gresources/nemo-icon-view-ui.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
<menuitem name="Sort by Type" action="Sort by Type"/>
1111
<menuitem name="Sort by Detailed Type" action="Sort by Detailed Type"/>
1212
<menuitem name="Sort by Modification Date" action="Sort by Modification Date"/>
13-
<menuitem name="Sort by Trash Time" action="Sort by Trash Time"/>
14-
</placeholder>
13+
<menuitem name="Sort by Trash Time" action="Sort by Trash Time"/>
14+
<menuitem name="Sort by Extension" action="Sort by Extension"/>
15+
</placeholder>
1516
<separator name="Layout separator"/>
1617
<menuitem name="Reversed Order" action="Reversed Order"/>
1718
</menu>
@@ -32,8 +33,9 @@
3233
<menuitem name="Sort by Type" action="Sort by Type"/>
3334
<menuitem name="Sort by Detailed Type" action="Sort by Detailed Type"/>
3435
<menuitem name="Sort by Modification Date" action="Sort by Modification Date"/>
35-
<menuitem name="Sort by Trash Time" action="Sort by Trash Time"/>
36-
</placeholder>
36+
<menuitem name="Sort by Trash Time" action="Sort by Trash Time"/>
37+
<menuitem name="Sort by Extension" action="Sort by Extension"/>
38+
</placeholder>
3739
<separator name="Layout separator"/>
3840
<menuitem name="Reversed Order" action="Reversed Order"/>
3941
</menu>

libnemo-private/nemo-column-utilities.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ get_builtin_columns (void)
144144
"label", _("MIME Type"),
145145
"description", _("The mime type of the file."),
146146
NULL));
147+
columns = g_list_append (columns,
148+
g_object_new (NEMO_TYPE_COLUMN,
149+
"name", "extension",
150+
"attribute", "extension",
151+
"label", _("Extension"),
152+
"description", _("The extension of the file."),
153+
NULL));
147154
#ifdef HAVE_SELINUX
148155
columns = g_list_append (columns,
149156
g_object_new (NEMO_TYPE_COLUMN,

libnemo-private/nemo-file.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ static GQuark attribute_name_q,
164164
attribute_link_target_q,
165165
attribute_volume_q,
166166
attribute_free_space_q,
167-
attribute_search_result_snippet_q,
168-
attribute_search_result_count_q;
167+
attribute_extension_q,
168+
attribute_search_result_snippet_q,
169+
attribute_search_result_count_q;
169170

170171
static void nemo_file_info_iface_init (NemoFileInfoInterface *iface);
171172

@@ -3226,6 +3227,48 @@ compare_by_type (NemoFile *file_1, NemoFile *file_2, gboolean detailed)
32263227
return result;
32273228
}
32283229

3230+
static int
3231+
compare_by_extension (NemoFile *file_1, NemoFile *file_2)
3232+
{
3233+
const char *name_1, *name_2;
3234+
char *ext_1, *ext_2;
3235+
char *ext_1_folded, *ext_2_folded;
3236+
int result;
3237+
3238+
name_1 = nemo_file_peek_display_name (file_1);
3239+
name_2 = nemo_file_peek_display_name (file_2);
3240+
3241+
ext_1 = eel_filename_get_extension_offset (name_1);
3242+
ext_2 = eel_filename_get_extension_offset (name_2);
3243+
3244+
if (ext_1 == NULL && ext_2 == NULL) {
3245+
return 0;
3246+
}
3247+
3248+
if (ext_1 == NULL) {
3249+
return -1;
3250+
}
3251+
3252+
if (ext_2 == NULL) {
3253+
return 1;
3254+
}
3255+
3256+
/* Skip the dot */
3257+
ext_1++;
3258+
ext_2++;
3259+
3260+
/* Case-insensitive comparison */
3261+
ext_1_folded = g_utf8_casefold (ext_1, -1);
3262+
ext_2_folded = g_utf8_casefold (ext_2, -1);
3263+
3264+
result = g_utf8_collate (ext_1_folded, ext_2_folded);
3265+
3266+
g_free (ext_1_folded);
3267+
g_free (ext_2_folded);
3268+
3269+
return result;
3270+
}
3271+
32293272
static int
32303273
compare_by_time (NemoFile *file_1, NemoFile *file_2, NemoDateType type)
32313274
{
@@ -3410,6 +3453,12 @@ nemo_file_compare_for_sort (NemoFile *file_1,
34103453
if (result == 0) {
34113454
result = compare_by_full_path (file_1, file_2);
34123455
}
3456+
break;
3457+
case NEMO_FILE_SORT_BY_EXTENSION:
3458+
result = compare_by_extension (file_1, file_2);
3459+
if (result == 0) {
3460+
result = compare_by_full_path (file_1, file_2);
3461+
}
34133462
break;
34143463
case NEMO_FILE_SORT_BY_MTIME:
34153464
result = compare_by_time (file_1, file_2, NEMO_DATE_TYPE_MODIFIED);
@@ -3500,6 +3549,13 @@ nemo_file_compare_for_sort_by_attribute_q (NemoFile *file_1,
35003549
favorites_first,
35013550
reversed,
35023551
search_dir);
3552+
} else if (attribute == attribute_extension_q) {
3553+
return nemo_file_compare_for_sort (file_1, file_2,
3554+
NEMO_FILE_SORT_BY_EXTENSION,
3555+
directories_first,
3556+
favorites_first,
3557+
reversed,
3558+
search_dir);
35033559
} else if (attribute == attribute_modification_date_q ||
35043560
attribute == attribute_date_modified_q ||
35053561
attribute == attribute_date_modified_with_time_q ||
@@ -6724,6 +6780,16 @@ nemo_file_get_string_attribute_q (NemoFile *file, GQuark attribute_q)
67246780
if (attribute_q == attribute_name_q) {
67256781
return nemo_file_get_display_name (file);
67266782
}
6783+
if (attribute_q == attribute_extension_q) {
6784+
const char *name;
6785+
char *ext;
6786+
name = nemo_file_peek_display_name (file);
6787+
ext = eel_filename_get_extension_offset (name);
6788+
if (ext) {
6789+
return g_strdup (ext + 1);
6790+
}
6791+
return NULL;
6792+
}
67276793
if (attribute_q == attribute_type_q) {
67286794
return nemo_file_get_type_as_string (file);
67296795
}
@@ -8959,6 +9025,7 @@ nemo_file_class_init (NemoFileClass *class)
89599025
attribute_link_target_q = g_quark_from_static_string ("link_target");
89609026
attribute_volume_q = g_quark_from_static_string ("volume");
89619027
attribute_free_space_q = g_quark_from_static_string ("free_space");
9028+
attribute_extension_q = g_quark_from_static_string ("extension");
89629029
attribute_search_result_snippet_q = g_quark_from_string ("search_result_snippet");
89639030
attribute_search_result_count_q = g_quark_from_string ("search_result_count");
89649031

libnemo-private/nemo-file.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ typedef enum {
6464
NEMO_FILE_SORT_BY_MTIME,
6565
NEMO_FILE_SORT_BY_ATIME,
6666
NEMO_FILE_SORT_BY_TRASHED_TIME,
67-
NEMO_FILE_SORT_BY_BTIME,
68-
NEMO_FILE_SORT_BY_SEARCH_RESULT_COUNT
69-
} NemoFileSortType;
70-
67+
NEMO_FILE_SORT_BY_BTIME,
68+
NEMO_FILE_SORT_BY_SEARCH_RESULT_COUNT,
69+
NEMO_FILE_SORT_BY_EXTENSION
70+
} NemoFileSortType;
7171
typedef enum {
7272
NEMO_REQUEST_NOT_STARTED,
7373
NEMO_REQUEST_IN_PROGRESS,

src/nemo-desktop-icon-grid-view.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ static const DesktopSortCriterion sort_criteria[] = {
109109
"Desktop Sort by Date",
110110
"modification date",
111111
NEMO_FILE_SORT_BY_MTIME
112+
},
113+
{
114+
"Desktop Sort by Extension",
115+
"extension",
116+
NEMO_FILE_SORT_BY_EXTENSION
112117
}
113118
};
114119

@@ -1085,7 +1090,11 @@ static const GtkRadioActionEntry desktop_sort_radio_entries[] = {
10851090
{ "Desktop Sort by Date", NULL,
10861091
N_("Date"), NULL,
10871092
NULL,
1088-
NEMO_FILE_SORT_BY_MTIME }
1093+
NEMO_FILE_SORT_BY_MTIME },
1094+
{ "Desktop Sort by Extension", NULL,
1095+
N_("Extension"), NULL,
1096+
NULL,
1097+
NEMO_FILE_SORT_BY_EXTENSION }
10891098
};
10901099

10911100
static const GtkActionEntry desktop_grid_entries[] = {

src/nemo-desktop-overlay.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ sync_controls (NemoDesktopOverlay *overlay,
207207
case NEMO_FILE_SORT_BY_MTIME:
208208
combo_id = "Desktop Sort by Date";
209209
break;
210+
case NEMO_FILE_SORT_BY_EXTENSION:
211+
combo_id = "Desktop Sort by Extension";
212+
break;
210213
case NEMO_FILE_SORT_BY_DISPLAY_NAME:
211214
default:
212215
combo_id = "Desktop Sort by Name";

src/nemo-icon-view.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,20 @@ static const SortCriterion sort_criteria[] = {
165165
N_("by Modification _Date"),
166166
N_("Keep icons sorted by modification date in rows")
167167
},
168-
{
169-
NEMO_FILE_SORT_BY_TRASHED_TIME,
170-
"trashed",
171-
"Sort by Trash Time",
172-
N_("by T_rash Time"),
173-
N_("Keep icons sorted by trash time in rows")
174-
}
168+
{
169+
NEMO_FILE_SORT_BY_TRASHED_TIME,
170+
"trashed",
171+
"Sort by Trash Time",
172+
N_("by T_rash Time"),
173+
N_("Keep icons sorted by trash time in rows")
174+
},
175+
{
176+
NEMO_FILE_SORT_BY_EXTENSION,
177+
"extension",
178+
"Sort by Extension",
179+
N_("by _Extension"),
180+
N_("Keep icons sorted by extension in rows")
181+
}
175182
};
176183

177184
static void nemo_icon_view_set_directory_sort_by (NemoIconView *icon_view,
@@ -1448,6 +1455,10 @@ static const GtkRadioActionEntry arrange_radio_entries[] = {
14481455
N_("By T_rash Time"), NULL,
14491456
N_("Keep icons sorted by trash time in rows"),
14501457
NEMO_FILE_SORT_BY_TRASHED_TIME },
1458+
{ "Sort by Extension", NULL,
1459+
N_("By _Extension"), NULL,
1460+
N_("Keep icons sorted by extension in rows"),
1461+
NEMO_FILE_SORT_BY_EXTENSION },
14511462
};
14521463

14531464
static void

0 commit comments

Comments
 (0)